function asyncPrint (message) { console.log(message)
}function pro (message, miniSecond) { return new Promise((resolve, reject) => {
setTimeout(asyncPrint, miniSecond, message)
})
}async function result() { console.log('before') await pro('printMMEE', 1000) console.log('after') console.log('sync code')
}
result()请问为什么这两行代码在await之后就不执行了呢?console.log('after')
console.log('sync code')
2 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
代码应该这么写:
function asyncPrint (message) { console.log(message) }function pro (message, miniSecond) { return new Promise((resolve, reject) => { // 注意下面的代码 setTimeout(() => { asyncPrint(message); resolve(message) }, miniSecond) }) }async function result() { console.log('before') await pro('printMMEE', 1000) console.log('after') console.log('sync code') }
添加回答
举报
0/150
提交
取消