JavaScript中的Event Loop
(function test() { setTimeout(function() {console.log(4)}, 0); new Promise(function executor(resolve) { console.log(1); for( var i=0 ; i<10000 ; i++ ) { i == 9999 && resolve(); } console.log(2); }).then(function() { console.log(5); }); console.log(3); })() 输出的结果是1,2,3,5,4。 首先 创建Promise实例(executor)是同步执行的,Promise.then是异步执行的。 从结果看setTimeout的异步和Promise.then的异步不一样。 先去查看Promise的规范 https://promisesaplus.com/ promise.then(onFulfilled, onRejected) 2.2.4 onFulfilled or onRejected must not be called until the execution…