domenic / zones

Former home of the zones proposal for JavaScript
205 stars 13 forks source link

A question about async await with native promise.(try to implement zone.js for async/await) #12

Open JiaLiPassion opened 6 years ago

JiaLiPassion commented 6 years ago

The following code

async function detect() { } const r = detect(); const nativeThen = r.proto.then; r.proto.then = function () { console.log('promise.then'); return nativeThen.apply(this, arguments); }

async function test1() { console.log('before await'); const r = await test(); console.log('after await'); }

test1(); console.log('end');

// the output will be // before await // end // promise.then, question is why promise.then is after end? // after await The nativePromise.then will be executed after end, I don't understand why, any help will be appreciated.

And What is the current status of zone proposal? Thanks!

danielgindi commented 5 years ago

This is because the Promise callbacks are always scheduled on the microtask queue. Which happens after the current execution context finishes.