es6 Promise 执行顺序

xiaoxiao2021-02-28  16

'use strict'; setTimeout(function () { console.log('three'); }, 0); new Promise(function(resolve,reject){ console.log(1); resolve(1); console.log(2); }).then(function () { console.log('two'); }); console.log('one');

new Promise时除了resolve或者reject(需要then或者catch,或者await)的代码都是在定义时立即执行。而.then,以及.catch会在microtask阶段执行,micorotask会在任意阶段执行之后立即执行完毕

'use strict'; new Promise(function (resolve, reject) { console.log('Promise'); resolve(); new Promise(function (resolve, reject) { console.log('Promise>>>'); resolve(); }).then(function () { console.log('resolved.>>>'); }) }).then(function () { console.log('resolved.'); })

另外可以看下mdn事件循环。

转载请注明原文地址: https://www.6miu.com/read-1450249.html

最新回复(0)