聚合错误:未解决 Promise.any 中的 Promise
当传递给 Promise.any()
的所有承诺都被拒绝时,会发生 JavaScript 异常 "未解决 Promise.any 中的 Promise"。它是 AggregateError
的唯一内置用法。
¥The JavaScript exception "No Promise in Promise.any was resolved" occurs when all promises passed to Promise.any()
are rejected. It is the only built-in usage of AggregateError
.
信息
错误类型
什么地方出了错?
¥What went wrong?
Promise.any()
仅在传递给它的所有承诺都被拒绝时才会拒绝。你应该访问 errors
以获取拒绝原因数组。有关如何处理异步拒绝的承诺的更多信息,请参阅 使用 promise。当 Promise.any()
收到一个空的可迭代对象时,也会引发此错误。
¥Promise.any()
only rejects when all promises passed to it are rejected. You should access errors
to get the array of rejection reasons. See Using promises for more information on how to handle asynchronously rejected promises. This error is also raised when Promise.any()
receives an empty iterable.
示例
空可迭代对象
处理所有拒绝
¥Handling all rejections
const promises = [
fetch("/data-location1"),
fetch("/data-location1"),
fetch("/data-location1"),
];
Promise.any(promises)
.then((value) => console.log(value))
.catch((error) => {
// None of the fetches were successful
for (const e of error.errors) {
console.error(e);
}
});
// Using await
async function fetchFirstSuccessful() {
try {
const value = await Promise.any(promises);
console.log(value);
} catch (error) {
for (const e of error.errors) {
console.error(e);
}
}
}
也可以看看
¥See also