简体   繁体   English

关于 Node.js Promise 然后返回?

[英]About Node.js Promise then and return?

I'm confused about Promise!我对 Promise 感到困惑!

I use Promise then without return like this:我使用Promise 然后没有像这样返回:

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 .我得到这个结果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 .我得到另一个结果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我得到这个结果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:如果您实际打印已解析的 promise 的值而不仅仅是变量的名称,则会更容易理解控制流:

Version 1版本 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版本 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版本 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结果 1

v1: 1
v2: undefined
v11: 2

Result 2结果 2

v1: 1
v11: 2
v2: undefined

Result 3结果 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:正如您在.then()处理程序中看到的那样.then()您没有返回承诺,它的作用就好像您返回了一个已解决且值为undefined承诺 - 就像您这样做了:

return Promise.resolve(undefined);

and thus the next .then() handler can be called immediately.因此可以立即调用下一个.then()处理程序。

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

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.这解释了当您不返回承诺时不同的执行顺序 - 发生的事情就好像已经解决的承诺为您隐式返回一样。

    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控制台三显示未定义,因为三个未解析解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM