Saturday, 28 January 2017

Race Condition in JavaScript Promises

I recently encountered a race condition in the use of my promises.

It seems trivial, but these little mistakes have a tendency to cause a lot of debugging time.
getUrls(): ng.IPromise<any> {
    if (this.urls != null) {
        var urls = this.urls;
        return this.$q(function (resolve, reject) { resolve(urls); });
    this.urls = {};
    var urls: any = this.urls;
    return this.$http.get("conf/urls.json").then(function (response: any) {
        urls.productionUrl =;
        urls.testingUrl =;
Can you spot the problem? The problem occurs when two threads access the same method at the same time.

Click here for the answer.

I always have problems wrapping my mind around JavaScript promises.

No comments:

Post a Comment