console.log('start') setTimeout(() => { console.log('setTimeout') }, 0) new Promise((resolve) => { console.log('promise') resolve() }) .then(() => { console.log('then1') }) .then(() => { console.log('then2') }) console.log('end')
![](https://common.cnblogs.com/images/loading.gif)
进阶版:
async function async1() { console.log('async1 start'); await async2(); console.log('async1 end'); } async function async2() { console.log('async2'); } console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0) async1(); new Promise(function(resolve) { console.log('promise1'); resolve(); }).then(function() { console.log('promise2'); }); console.log('script end');
高级版:
console.log('start'); setTimeout(() => { console.log('children2'); Promise.resolve().then(() => { console.log('children3'); }) }, 0); new Promise(function(resolve, reject) { console.log('children4'); setTimeout(function() { console.log('children5'); resolve('children6') }, 0) }).then((res) => { console.log('children7'); setTimeout(() => { console.log(res); }, 0) })
知识补充:https://zhuanlan.zhihu.com/p/145383822