const myPromise = () => Promise.resolve('I have resolved!')
function firstFunction() {
myPromise().then(res => console.log(res))
console.log('second')
}
async function secondFunction() {
console.log(await myPromise())
console.log('second')
}
firstFunction()
secondFunction()
A: I have resolved!, second and I have resolved!, second
B: second, I have resolved! and second, I have resolved!
C: I have resolved!, second and second, I have resolved!
D: second, I have resolved! and I have resolved!, second
答案: D
遇到await myPromise(),就是一个异步了,它阻断了后面代码的执行,
所以先执行firstFunction中的second,然后firstFunction中微任务然后再是secondFunction的微任务
async function getData() {
return await Promise.resolve("I made it!");
}
const data = getData();
console.log(data);
A: "I made it!"
B: Promise {<resolved>: "I made it!"}
C: Promise {<pending>}
D: undefined
答案:C
全局执行上下文调用异步getData后,接着执行console.log。这是执行的顺序 getData调用的结果是返回pending状态的Promise