记两题事件循环


console.log('1');//1.1

setTimeout(function() {//1.宏1
    console.log('2');//3.2
    process.nextTick(function() {//3.微1 
        console.log('3');//4.3
    })
    new Promise(function(resolve) {
        console.log('4');//3.4
        resolve();//3.微2
    }).then(function() {
        console.log('5')//4.5
    })
})
process.nextTick(function() {//1.微1
    console.log('6');//2.6
})
new Promise(function(resolve) {
    console.log('7');//1.7
    resolve();//1.微2
}).then(function() {
    console.log('8')//2.8
})

setTimeout(function() {//1.宏2
    console.log('9'); // 5.9
    process.nextTick(function() {//5.微1
        console.log('10');//6.10
    })
    new Promise(function(resolve) {
        console.log('11');//5.11
        resolve();//5.微2
    }).then(function() {
        console.log('12')//6.12
    })
})

// 1 7 6 8 2 4 3 5 9 11 10 12
  • 先顺序,异步分别如微任务宏任务队列
  • 先把微任务清空,在考虑宏任务
  • 每次完成一个宏任务,就去微任务里面看看有没有,又要先做

function testSometing() {
    console.log("testSomething");//1.2
    return "return testSomething";//微1
}

async function testAsync() {
    console.log("testAsync");//2.2
    return Promise.resolve("hello async");//promise 微3
}

async function test() {
    console.log("test start...");//1.1

    const testFn1 = await testSometing(); //1.先执行一遍然后跳出
    console.log(testFn1);//2.1

    const testFn2 = await testAsync();//2.先执行一遍然后跳出
    console.log(testFn2);//3.1

    console.log('test end...');//3.2
}

test();

var promiseFn = new Promise((resolve)=> { 
                    console.log("promise START...");//1.3
                    resolve("promise RESOLVE");//1.微2
                });
promiseFn.then((val)=> console.log(val));//2.3

console.log("===END===")//1.4

/*
正解
test start...
testSomething
promise START...
===END===
return testSomething
testAsync
promise RESOLVE
hello async
test end...
*/



/*
错误答案
test start...
promise START...
===END===
testSomething
return testSomething
promise RESOLVE
testAsync
test end...
hello async
*/

/*
错误答案
test start...
testSomething
return testSomething
testAsync
test end...
promise START...
===END===
hello async
promise RESOLVE
*/
  • await要先执行一遍,然后跳出async执行别的,再回来
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值