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