简体   繁体   中英

why my async code is not executing inspite of being the free call stack

As per my knowledge async code is executed when the call stack is empty and execution of the async code is finished in web API

  • But In a given code why my async code which is setTimeout function and Promise which resolve quickly -- is not getting executed at the mark point <---

  • at this point call stack is also empty and async code might also have been executed

  • Function fu() is used as a delay so that async code should executed until <----

but output is

after 1 
after 1
Test End
Resolve promise1
0 sec timer

 console.log('test start'); setTimeout(() => console.log('0 sec timer'), 0); Promise.resolve('Resolve promise1').then(res => { mk = 20; console.log(res); }); fu(); fu(); // <<--------- console.log('Test End');

As I am in learning stage of JavaScript so please tell me if I am lagging in tech detail of asynchronous js

No, the call stack is not yet empty at the point you marked. It still contains the global execution context that is running your <script> . Every script or module is executed in one go, synchronously without interruption, just like you would expect it for the execution of a dom event listener or setTimeout callback or promise then handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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