为什么这个 JavaScript ES6 例子中 "await" 之后的代码没有得到 运行?
Why doesn't the code after "await" in this JavaScript ES6 example get run?
我有以下片段:
let zxcv = () => new Promise((resolve, reject) => {
setTimeout(() => {
console.log(4)
}, 1000)
})
async function jk(){
await zxcv()
console.log(6)
}
jk()
当我 运行 这段代码时,它会等待一秒钟,然后记录“4”,然后结束。 “6”永远不会被记录。
为什么?
您从未在 zxcv
兑现您的承诺:
let zxcv = () => new Promise((resolve, reject) => {
setTimeout(() => {
console.log(4)
resolve()
}, 1000)
})
否则promise将无限期挂起!
我有以下片段:
let zxcv = () => new Promise((resolve, reject) => {
setTimeout(() => {
console.log(4)
}, 1000)
})
async function jk(){
await zxcv()
console.log(6)
}
jk()
当我 运行 这段代码时,它会等待一秒钟,然后记录“4”,然后结束。 “6”永远不会被记录。
为什么?
您从未在 zxcv
兑现您的承诺:
let zxcv = () => new Promise((resolve, reject) => {
setTimeout(() => {
console.log(4)
resolve()
}, 1000)
})
否则promise将无限期挂起!