About Node.js Promise then and return?

穿精又带淫゛_ 提交于 2021-02-19 06:10:42

问题


I'm confused about Promise!

I use Promise then without return like this:

new Promise((resolve, reject) => {
    resolve("1");
}).then((v1) => {
    console.log("v1");
    new Promise((resolve, reject) => {
        //Time-consuming operation, for example: get data from database;
        setTimeout(() => {
            resolve(2)
        }, 3000);
    }).then((v11) => {
        console.log("v11");
    })
}).then((v2) => {
    console.log("v2")
});

I get this result v1 v2 v11. Then, I use another way of writing, Like below:

 new Promise((resolve, reject) => {
    resolve("1");
}).then((v1) => {
    console.log("v1");
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(2)
        }, 3000)
    }).then((v11) => {
        console.log("v11");
    })
}).then((v2) => {
    console.log("v2")
});

I get another result v1 v11 v2.

Maybe, There is another case:

new Promise((resolve, reject) => {
resolve("1");
}).then((v1) => {
console.log("v1");
return new Promise((resolve, reject) => {
    setTimeout(() => {resolve(2)}, 3000)
}).then((v11) => {
    console.log("v11");
    return new Promise((resolve, reject) => {
        setTimeout(() => {resolve(2)}, 4000)
    }).then(() => {
        console.log("v12")
    })
})
}).then((v2) => {
console.log("v2")
});

I get this result v1 v11 v12 v2

I can't understand the second return I want to know why I get this result?


回答1:


It will be easier to understand the control flow if you actually print the values of the resolved promises and not only the names of the variables:

Version 1

new Promise((resolve, reject) => {
    resolve("1");
}).then((v1) => {
    console.log("v1:", v1);
    new Promise((resolve, reject) => {
        //Time-consuming operation, for example: get data from database;
        setTimeout(() => {
            resolve(2)
        }, 3000);
    }).then((v11) => {
        console.log("v11:", v11);
    })
}).then((v2) => {
    console.log("v2:", v2)
});

Version 2

new Promise((resolve, reject) => {
    resolve("1");
}).then((v1) => {
    console.log("v1:", v1);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(2)
        }, 3000)
    }).then((v11) => {
        console.log("v11:", v11);
    })
}).then((v2) => {
    console.log("v2:", v2)
});

Version 3

new Promise((resolve, reject) => {
resolve("1");
}).then((v1) => {
console.log("v1:", v1);
return new Promise((resolve, reject) => {
    setTimeout(() => {resolve(2)}, 3000)
}).then((v11) => {
    console.log("v11:", v11);
    return new Promise((resolve, reject) => {
        setTimeout(() => {resolve(2)}, 4000)
    }).then((v12) => {
        console.log("v12:", v12)
    })
})
}).then((v2) => {
console.log("v2:", v2)
});

Now you can see what gets passed to the callbacks:

Result 1

v1: 1
v2: undefined
v11: 2

Result 2

v1: 1
v11: 2
v2: undefined

Result 3

v1: 1
v11: 2
v12: 2
v2: undefined

Explanation

As you can see when in the .then() handlers you don't return a promise, it acts as if you returned an already resolved promise with value undefined - like if you did:

return Promise.resolve(undefined);

and thus the next .then() handler can be called immediately.

If, on the other hand, you return a promise that is not resolved yet, then the next .then() handler will not be invoked immediately but only after that returned promise gets resolved.

And that explains the order of execution that is different when you don't return a promise - and what happens is as if an already resolved promise got returned implicitly for you.




回答2:


    function one() {
    return new Promise((resolve,reject) => {
        setTimeout(function () {
            console.log("one 1 ");
            resolve('one one');
        }, 2000);
    });
}

function two() {
    return new Promise((resolve,reject) => {
        setTimeout(function () {
            console.log("two 2 ");
            resolve('two two');
        }, 10000);
    });
}

function three(){
    setTimeout(function () {
        console.log("three 3 ");
    }, 5000);
}

one().then((msg) => {
        console.log('one : ', msg);
        return two();
    }).then((msg) => {
        console.log('two :', msg);
        return three();
    }).then((msg) => {
        console.log('three :', msg);
    })
    .catch((error) => {
        console.error('Something bad happened:', error.toString());
    });

console three show undefined because three not parse resolve



来源:https://stackoverflow.com/questions/43140118/about-node-js-promise-then-and-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!