Skip to content Skip to sidebar Skip to footer

Ensuring Asyncronous '.then()' Execution In Compound Promises

I am using Typescript to write JS promises. I have a compound promise, i.e. promise which returns a promise I have: test() { addElement('first') .then( func

Solution 1:

You're not returning your promise in the then block. If you don't return the promise, then the then block will return undefined immediately, and nothing will wait for it. If you return a promise instead, then execution of the promise chain will pause until the promise you return has been satisfied.

test() {
    addElement("first")
        .then(
            function (val){
                console.log("second*");
                returnaddElement("second");
            })
            .then(
            function (val){
                console.log("third*");
                returnaddElement("third");
            });
}

Though I don't personally consider it a "rookie mistake", your description matches "Rookie Mistake #5" in Nolan Lawson's "We have a problem with promises"; read on for a more in-depth explanation.

Post a Comment for "Ensuring Asyncronous '.then()' Execution In Compound Promises"