functionfoo(){bar();
console.log('foo');}functionbar(){baz();
console.log('bar');}functionbaz(){
console.log('baz');}foo();/*
baz
bar
foo
*/
2. 有微任务时
使用new关键字创建的promise传递的函数会自动执行,.then里面的才是异步任务
functionfoo(){
console.log('foo');}
console.log('global start');newPromise((resolve, reject)=>{resolve();
console.log('promise');}).then(()=>{
console.log('promise then');}).catch(()=>{
console.log('error');})foo();
console.log('global end');/*
global start
promise
foo
global end
promise then
*/
3. 有微任务&宏任务时
functionfoo(){
console.log('foo');}
console.log('global start');setTimeout(()=>{
console.log('setTimeout: 0s');},0);newPromise(resolve=>{
console.log('promise');resolve();}).then(()=>{
console.log('promise then');})foo();
console.log('global end');/*
global start
promise
foo
global end
promise then
setTimeout: 0s
*/