关于 Node.js Promise 然后返回? [英] About Node.js Promise then and return?

查看:46
本文介绍了关于 Node.js Promise 然后返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Promise 感到困惑!

我使用 Promise then 没有像这样返回:

new Promise((resolve, reject) => {解决(1");}).then((v1) => {console.log("v1");新承诺((解决,拒绝)=> {//耗时操作,例如:从数据库中获取数据;setTimeout(() => {解决(2)}, 3000);}).then((v11) => {CONSOLE.LOG( V11");})}).then((v2) => {console.log("v2")});

我得到了这个结果 v1 v2 v11.然后,我使用另一种写法,如下所示:

 new Promise((resolve, reject) => {解决(1");}).then((v1) => {console.log("v1");返回新的承诺((解决,拒绝)=> {setTimeout(() => {解决(2)}, 3000)}).then((v11) => {console.log("v11");})}).then((v2) => {console.log("v2")});

我得到另一个结果v1 v11 v2.

也许,还有一种情况:

new Promise((resolve, reject) => {解决(1");})然后((V1)=> {console.log("v1");返回新的承诺((解决,拒绝)=> {setTimeout(() => {resolve(2)}, 3000)}).then((v11) => {console.log("v11");返回新的承诺((解决,拒绝)=> {setTimeout(() => {resolve(2)}, 4000)}).then(() => {console.log("v12")})})}).then((v2) => {console.log("v2")});

我得到这个结果v1 v11 v12 v2

我无法理解第二个返回我想知道为什么我得到这个结果?

解决方案

如果您实际打印已解决的 promise 的值,而不仅仅是变量的名称,则会更容易理解控制流:

版本1

new Promise((resolve, reject) => {解决(1");}).then((v1) => {console.log("v1:", v1);新承诺((解决,拒绝)=> {//耗时操作,例如:从数据库中获取数据;setTimeout(() => {解决(2)}, 3000);}).then((v11) => {console.log("v11:", v11);})}).then((v2) => {console.log("v2:", v2)});

版本 2

new Promise((resolve, reject) => {解决(1");}).then((v1) => {console.log("v1:", v1);返回新的承诺((解决,拒绝)=> {setTimeout(() => {解决(2)}, 3000)})然后((V11)=> {console.log("v11:", v11);})}).then((v2) => {console.log("v2:", v2)});

版本 3

new Promise((resolve, reject) => {解决(1");}).then((v1) => {console.log("v1:", v1);返回新的承诺((解决,拒绝)=> {setTimeout(() => {resolve(2)}, 3000)}).then((v11) => {console.log("v11:", v11);返回新的承诺((解决,拒绝)=> {setTimeout(() => {resolve(2)}, 4000)}).then((v12) => {console.log("v12:", v12)})})}).then((v2) => {console.log("v2:", v2)});

现在您可以看到传递给回调的内容:

结果1

v1: 1v2:未定义v11:2

结果 2

v1: 1v11:2v2:未定义

结果 3

v1: 1v11:2v12:2v2:未定义

说明

正如您在 .then() 处理程序中所看到的,当您没有返回承诺时,它的行为就像您返回了一个已解析的承诺,其值为 undefined- 就像你这样做:

<预> <代码>返回Promise.resolve(未定义);

因此可以立即调用下一个 .then() 处理程序.

另一方面,如果您返回一个尚未解决的承诺,那么下一个 .then() 处理程序将不会被立即调用,而只会在返回的承诺得到解决之后才会被调用.

这就解释了当你不返回承诺时不同的执行顺序 - 发生的事情就好像一个已经解决的承诺为你隐式返回一样.

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?

解决方案

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.

这篇关于关于 Node.js Promise 然后返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆