e005-执行打印005
async function async1() {
console.log('A')
await async2()
console.log('B')
}
async function async2() {
console.log('C')
}
console.log('D')
setTimeout(function() {
console.log('E')
})
async1()
new Promise(function(resolve) {
console.log('F')
}).then(function() {
console.log('G')
}).catch(() => {
console.log('G2')
})
console.log('H')
答案
// 同步代码
D
A
C
F
H
//微任务执行
B
undefined
// 宏任务执行
E
async function async1() {
console.log('A')
await async2()
console.log('B')
}
async function async2() {
console.log('C')
}
console.log('D')
setTimeout(function() {
console.log('E')
})
async1()
new Promise(function(resolve) {
console.log('F')
resolve(1)
}).then(function() {
console.log('G')
}).catch(() => {
console.log('G2')
})
console.log('H')
答案
// 同步代码
D
A
C
F
H
//微任务执行
B
G
undefined
// 宏任务执行
E
3
setTimeout(function () {
console.log("1");
}, 0);
async function async1() {
console.log("2");
const data = await async2();
console.log("3");
return data;
}
async function async2() {
return new Promise((resolve) => {
console.log("4");
resolve("async2的结果");
}).then((data) => {
console.log("5");
return data;
});
}
async1().then((data) => {
console.log("6");
console.log(data);
});
new Promise(function (resolve) {
console.log("7");
// resolve()
}).then(function () {
console.log("8");
});
注意8那的 // resolve() 注掉了
输出
247536 async2 的结果 1
4.
setTimeout(function () {
console.log("1");
}, 0);
async function async1() {
console.log("2");
const data = await async2();
console.log("3");
return data;
}
async function async2() {
return new Promise((resolve) => {
console.log("4");
resolve("async2的结果");
}).then((data) => {
console.log("5");
return data;
});
}
async1().then((data) => {
console.log("6");
console.log(data);
});
new Promise(function (resolve) {
console.log("7");
resolve()
}).then(function () {
console.log("8");
});
输出
2475836 async2 的结果 1