Promise和Promise链如何工作?

我正在学习Node.js,并尝试正确使用mysql2模块。因此,我最近开始研究诺言。


我正在写一种“库”,所以我可以练习所有这些主题,而在这样做的时候,我遇到了我无法真正理解的承诺链问题。任何帮助,我们将不胜感激!


问题如下:


假设我有一个query函数,该函数可获取数据库,处理数据并返回Promise,因此我可以获取该数据并在其他文件中使用它。


现在,如果我这样写我的query函数:



query(){

        let p = new Promise((resolve, reject) => {

            resolve("Hello world")

        });



        p.then(data => {

            console.log("Hello world a second time!");

        }).then(data => {

            console.log("Hello world a third time")

        })

        return p;

    }


并且我尝试像这样从其他文件中“消费”该承诺:



DBObject.query().then((data) => {

    console.log("Hello world from the other file!");

})


然后输出顺序错误,程序将输出以下内容:


Hello world a second time!

Hello world from the other file!

Hello world a third time


另一方面,如果我更改第一个文件中的代码,并且不尝试分开promise链,如下所示:


query(){

        let p = new Promise((resolve, reject) => {

            resolve("Hello world")

        }).then(data => {

            console.log("Hello world a second time!");

        }).then(data => {

            console.log("Hello world a third time")

        })


        return p;

    }


它工作正常,并且可以打印:


Hello world a second time!

Hello world a third time

Hello world from the other file!


我不了解这种行为,我当时想then与诺言定义分开声明块与声明诺言时对诺言链接的权利是同一回事,而且显然不是那样的!


预先感谢您可以给我的答案。另外,如果您能给我一些有关如何正确编写这样的代码的建议,那将是很棒的。我的意思是,如果我编写使用Promise的代码,应该返回给用户什么?另一个承诺?还是只是供他们使用的数据?我真的很想编写遵循“标准”做事方式的代码。


忽然笑
浏览 160回答 3
3回答

慕侠2389804

当您有一个Promise时,可以将任意数量的Promises链接到其上.then。例如const p = Promise.resolve();p.then(() => console.log('then 1');p.then(() => console.log('then 2');表示在解决时p有两个从其分支的Promises:1和2(除了Promisep本身)。&nbsp; p&nbsp;/ \/&nbsp; &nbsp;\1&nbsp; &nbsp;2您在第一个代码中正在做什么let p = new Promise((resolve, reject) => {&nbsp; resolve("Hello world")});p.then(data => {&nbsp; console.log("second");}).then(data => {&nbsp; console.log("third")})return p;就好像"Hello world" = <Promise you return>&nbsp; &nbsp; |&nbsp; &nbsp; |&nbsp; &nbsp; |&nbsp; second&nbsp; &nbsp; |&nbsp; &nbsp; |&nbsp; &nbsp; |&nbsp; third = <unused Promise expression that the then chain resolves to>您有两个分支:返回的Promise在Hello world运行时解析,而不是在third运行时解析。另一方面,当您.then多次在Promise上调用时,整个表达式将计算为Promise,该Promise在最终.then运行时解析:let p = new Promise((resolve, reject) => {&nbsp; resolve("Hello world")}).then(data => {&nbsp; console.log("Hello world a second time!");}).then(data => {&nbsp; console.log("Hello world a third time")})return p;就好像"Hello world"&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp;|'Hello second'&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp;|'Hello third' = <Promise you return>返回的Promise是Hello third运行后立即解决的Promise 。

缥缈止盈

而不是返回p单独的版本,而是返回p.then()链。最后一个将添加到该链的末尾,而不是创建两个不同的链then()返回通过任何退货或undefined如果没有退货解决的新承诺const query = () => {&nbsp; let p = new Promise((resolve, reject) => {&nbsp; &nbsp; resolve("Hello world")&nbsp; });&nbsp; return p.then(data => {&nbsp; &nbsp; // from `resolve()`&nbsp; &nbsp; console.log('Then 1: ', data)&nbsp; &nbsp; // return to next then()&nbsp; &nbsp; return ("Hello world a second time!");&nbsp; }).then(data => {&nbsp; &nbsp; // from previous then()&nbsp; &nbsp; console.log('Then 2: ', data)&nbsp; &nbsp; // return to next then()&nbsp; &nbsp; return ("Hello world a third time")&nbsp; });}query().then(res => console.log('Final: ', res))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript