学学 Promise
Timing
function passed to .then
will never be called synchronously,even with an already-resolved promise.
1 | Promise.resolve().then(() => console.log(2)); |
instead of running immediately, the passed-in function is put on a microtask queue, which means it runs later when the queue is emptied at the end of the current run of the Javascript event loop.
相对于立即调用,被传入的回调函数会被放置到一个 microtask
队列中,这意味着只有当当前的事件循环结束后才会被调用。