gpt4 book ai didi

javascript - 未处理的 promise 拒绝?

转载 作者:搜寻专家 更新时间:2023-11-01 05:19:26 26 4
gpt4 key购买 nike

This example (repl.it) (来自 this answer )在我看来它遵循了所有关于 promise 的规则。然而,运行它会记录有关未处理的 promise 拒绝的异常以及相关的控制台消息。 (这也发生在 FF、Chrome 和 Node v10 中。)

try/catch block 显然在那里并包装了被拒绝的 promise ,那么这是怎么回事,我该如何解决它?

async function example() {
const start = Date.now()
let i = 0
function res(n) {
const id = ++i
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
console.log(`res #${id} called after ${n} milliseconds`, Date.now() - start)
}, n)
})
}

function rej(n) {
const id = ++i
return new Promise((resolve, reject) => {
setTimeout(() => {
reject()
console.log(`rej #${id} called after ${n} milliseconds`, Date.now() - start)
}, n)
})
}

try {
const delay1 = res(3000)
const delay2 = res(2000)
const delay3 = rej(1000)

const data1 = await delay1
const data2 = await delay2
const data3 = await delay3
} catch (error) {
console.log(`await finished`, Date.now() - start)
}
}

example()

最佳答案

问题在于,rej调用拒绝,解释器还没有到达 await 的那一行这是 rej 创建的 promise ,所以被拒绝的 Promise 就是一个被拒绝的 Promise,而不是 当前线程是 await 的 Promise。 ing:

try {
const delay1 = res(3000)
const delay2 = res(2000)
const delay3 = rej(1000)

const data1 = await delay1
// The interpreter is paused on the line above when `rej` rejects
const data2 = await delay2
const data3 = await delay3

因此,行为与在没有 catch 的情况下声明被拒绝的 Promise 相同。处理程序。 (Promises 抛出的错误只会在 async 函数中被捕获,如果它们是 await ed 在 Promise 拒绝的点 - 否则,它只会导致未处理的 promise拒绝。)

我建议在您 await 的同一点上声明 Promises。他们:

const data1 = await res(3000)

(注意:上述方法的时间不会和原来的代码一样)

或使用 await Promise.all对于所有 Promise,这意味着 Promise口译员目前是await一旦 一个 Promise 拒绝,ing 将抛出(并因此进入 catch block ):

const [data1, data2, data3] = await Promise.all([
res(3000),
res(2000),
rej(1000)
]);

async function example() {
const start = Date.now()
let i = 0
function res(n) {
const id = ++i
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
console.log(`res #${id} called after ${n} milliseconds`, Date.now() - start)
}, n)
})
}

function rej(n) {
const id = ++i
return new Promise((resolve, reject) => {
setTimeout(() => {
reject()
console.log(`rej #${id} called after ${n} milliseconds`, Date.now() - start)
}, n)
})
}

try {
const [data1, data2, data3] = await Promise.all([
res(3000),
res(2000),
rej(1000),
]);
} catch (error) {
console.log(`error caught: await finished`, Date.now() - start)
}
}

example()

要在三个 Promise 进行时做额外的工作,并从这些 Promise 和主线程中捕获错误,将第四个项传递给 Promise.all ,一个 IIFE,它可以完成您想做的额外工作:

async function example() {
const start = Date.now()
let i = 0
function res(n) {
const id = ++i
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
console.log(`res #${id} called after ${n} milliseconds`, Date.now() - start)
}, n)
})
}

function rej(n) {
const id = ++i
return new Promise((resolve, reject) => {
setTimeout(() => {
reject()
console.log(`rej #${id} called after ${n} milliseconds`, Date.now() - start)
}, n)
})
}

try {
const [data1, data2, data3] = await Promise.all([
res(3000),
res(2000),
rej(1000),
(() => {
console.log('doing work...');
})()
]);
} catch (error) {
console.log(`error caught: await finished`, Date.now() - start)
}
}

example()

关于javascript - 未处理的 promise 拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53243811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com