Study on the working mechanism of async/await--nodejs

Source: Internet
Author: User

Async/await in ES6 makes promise easier, usually the chain promise of await processing is wrapped in a function, and the returned result is still a promise object.

But when an await deals directly with chained promise, does the compiler return a promise object waiting for the next await, or will it continue executing the Promise object until the return result is no longer promise?

Test environment

NodeJS v8.7.0

Test code

1Const UTIL = require (' Util ');2 3Const PROM2 = Util.promisify ((A, b, cb) = = {4cbNULL, A +b);5 });6 7 functionPromisecal (A, b) {8     returnProm2 (A, b)9. Then (val) = {TenConsole.log (' ValueinchFirst then : ${val} '); One             returnVal * 2; A         }) -. Then (val) = { -Console.log (' Valueinchsecond then: ${val} '); the             returnVal * 3 -         }); - } -  +AsyncfunctionTest () { -     //await getting value from chained promise +Let Val1 = await promisecal (2, 4); A  atConsole.log (' Typeof val1: ${typeofVal1}\nfinal returned value: ${val1} '); -  -     //await getting value from another form -Console.log (' [v2] Type of prom2: ${typeofprom2} '); -Let Val2 = await prom2 (4, 6) -. Then (val) = { inConsole.log (' [v2] ValueinchFirst then : ${val} '); -             returnVal * 2; to         }) +. Then (val) = { -Console.log (' [v2] Valueinchsecond then: ${val} '); the             returnVal * 3 *         }); $Console.log (' [v2] Typeof val2: ${typeofVAL2}\N[V2] Final returned value: ${val2} ');Panax Notoginseng } -  the Test () +.Catch(E = console.error (E.stack | | e));

Run results

Value in first then:6value in second then:12typeof Val1:numberfinal returned VALUE:36[V2] Type of Prom2:function[v2] Value in first Then:10[v2] value in second THEN:20[V2] Typeof Val2:number[v2] Final returned value:60

Conclusion

The await operator is processed along the promise chain until the return result is no longer the promise position, and the await statement returns the return value of the last then function on the primise chain (or throws an exception).

Study on the working mechanism of async/await--nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.