async await promise setTimeout输出顺序问题

 这里关于异步操作的有一些歧义,一下的代码在QQ浏览器和谷歌浏览器中运行的顺序是不同的,但是只有异步微队列中的输出不同

//请写出输出内容
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');
/*
1. script start
2. async1 start
3. async2
4. promise1
5. script end
6. async1 end
7. script end
8. promise2
9. setTimeout
*/
async function async1() {
    console.log('async1 start');
    await async2();
    console.log('async1 end');
}
async function async2() {
    //async2做出如下更改:
    new Promise(function(resolve) {
    console.log('promise1');
    resolve();
}).then(function() {
    console.log('promise2');
    });
}
console.log('script start');

setTimeout(function() {
    console.log('setTimeout');
}, 0)
async1();

new Promise(function(resolve) {
    console.log('promise3');
    resolve();
}).then(function() {
    console.log('promise4');
});

console.log('script end');
//
script start
VM437:2 async1 start
VM437:9 promise1
VM437:23 promise3
VM437:29 script end
VM437:12 promise2
VM437:26 promise4
VM437:4 async1 end
undefined
VM437:18 setTimeout
async function async1() {
    console.log('async1 start');
    await async2();
    //更改如下:
    setTimeout(function() {
        console.log('setTimeout1')
    },0)
}
async function async2() {
    //更改如下:
    setTimeout(function() {
        console.log('setTimeout2')
    },0)
}
console.log('script start');

setTimeout(function() {
    console.log('setTimeout3');
}, 0)
async1();

new Promise(function(resolve) {
    console.log('promise1');
    resolve();
}).then(function() {
    console.log('promise2');
});
console.log('script end');
//
script start
VM434:2 async1 start
VM434:23 promise1
VM434:28 script end
VM434:26 promise2
undefined
VM434:18 setTimeout3
VM434:12 setTimeout2
VM434:6 setTimeout1
async function a1 () {
    console.log('a1 start')
    await a2()
    console.log('a1 end')
}
async function a2 () {
    console.log('a2')
}

console.log('script start')

setTimeout(() => {
    console.log('setTimeout')
}, 0)

Promise.resolve().then(() => {
    console.log('promise1')
})

a1()

let promise2 = new Promise((resolve) => {
    resolve('promise2.then')
    console.log('promise2')
})

promise2.then((res) => {
    console.log(res)
    Promise.resolve().then(() => {
        console.log('promise3')
    })
})
console.log('script end')

//
script start
VM440:2 a1 start
VM440:7 a2
VM440:24 promise2
VM440:33 script end
VM440:17 promise1
VM440:28 promise2.then
VM440:30 promise3
VM440:4 a1 end
undefined
VM440:13 setTimeout
//
script start
VM316:2 a1 start
VM316:7 a2
VM316:24 promise2
VM316:33 script end
VM316:17 promise1
VM316:4 a1 end
VM316:28 promise2.then
VM316:30 promise3
undefined
VM316:13 setTimeout

猜你喜欢

转载自blog.csdn.net/weixin_43797908/article/details/108561332