为什么.catch()不会在异步函数的循环内捕获Promise构造函数中的reject(),除非传递错误? [英] Why does .catch() not catch reject() within Promise constructor within loop at an async function unless Error is passed?
问题描述
给定
(async()=> {const p =等待新的Promise((resolve,reject)=> setTimeout(()=> {reject(new Error(1))},Math.floor(Math.random()* 1000))); return p})( ).then(data => console.log(data))。catch(err => console.error(err));
在 .catch()
如果我们扩展模式以使用循环,则错误
记录在 .catch()
const fn = async(res,... props)=> {for(let prop of props)res.push(await prop())return res} const arr = [()=> new Promise((resolve,reject)=> setTimeout(()=> reject(new Error(1)),Math.floor(Math.random()* 1000))),()=> new Promise((resolve,reject)=> setTimeout(()=> resolve(1),Math.floor(Math.random()* 1000)))]; fn([],... arr)。然后(data => console.log(data))。catch(err => console.log(err));
如果我们使用一个循环来调用多个返回 Promise
的函数,并且没有显式传递错误()
到 reject()
of Promise
constructor resolver
function .catch()
没有捕获到错误,数组 res
未返回,只有传递给 resolve()
的 Promise
值可在
const fn = async(res,... props)=> {for(let prop of props)res.push(await prop())return res} const arr = [()=> new Promise((resolve,reject)=> setTimeout(()=> reject(1),Math.floor(Math.random()* 1000))),()=>新的承诺((解决,拒绝)=> setTimeout(()=> resolve(1),Math.floor(Math.random()* 1000)))]; fn([],... arr)/ /我们从'fn`返回的数组`res`发生了什么?.then(data => console.log(data))//我们被拒绝的Promise发生了什么?.catch(err => console.log(err) ));
问题:
-
为什么
reject()
调用不会传播到.catch()
何时错误()
未在承诺中明确传递给
reject()
/ code>async
函数循环中的构造函数? -
为什么只有一个单个
Promise
返回到.then()
但是从async <返回一个数组/ code>函数当一个
Promise
对象在一个循环中迭代reject()
函数被调用承诺
构造函数解析器
函数?
如果我们[...]没有显式传递
错误()
到拒绝()
的Promise构造函数解析器函数.catch()
没有捕获错误
当然,因为没有错误恩。但 catch
将捕获您传递给 reject
的任何内容,这是值 1
,确实记录正确。
Given
(async () => {
const p = await new Promise((resolve, reject) => setTimeout(() => {
reject(new Error(1))
}, Math.floor(Math.random() * 1000))); return p})()
.then(data => console.log(data))
.catch(err => console.error(err));
the Error()
is logged at .catch()
If we extend the pattern to use a loop the Error
is logged at .catch()
const fn = async(res, ...props) => {
for (let prop of props) res.push(await prop())
return res
}
const arr = [
() =>
new Promise((resolve, reject) =>
setTimeout(() =>
reject(new Error(1))
, Math.floor(Math.random() * 1000))
),
() =>
new Promise((resolve, reject) =>
setTimeout(() =>
resolve(1)
, Math.floor(Math.random() * 1000))
)
];
fn([], ...arr)
.then(data => console.log(data))
.catch(err => console.log(err));
If we use a loop to call more than one function which returns a Promise
and do not explicitly pass Error()
to reject()
of Promise
constructor resolver
function .catch()
does not catch the error, and array res
is not returned, only the Promise
value passed to resolve()
is available at
const fn = async(res, ...props) => {
for (let prop of props) res.push(await prop())
return res
}
const arr = [
() =>
new Promise((resolve, reject) =>
setTimeout(() =>
reject(1)
, Math.floor(Math.random() * 1000))
),
() =>
new Promise((resolve, reject) =>
setTimeout(() =>
resolve(1)
, Math.floor(Math.random() * 1000))
)
];
fn([], ...arr)
// what happened to our array `res` returned from `fn`?
.then(data => console.log(data))
// what happened to our rejected Promise?
.catch(err => console.log(err));
Questions:
Why does
reject()
call not propagate to.catch()
whenError()
is not explicitly passed toreject()
withinPromise
constructor within a loop at anasync
function?Why is only a single
Promise
value returned to.then()
though an array is returned from theasync
function when one of thePromise
objects iterated at a loopreject()
function is called withinPromise
constructorresolver
function?
If we […] do not explicitly pass
Error()
toreject()
of Promise constructor resolver function.catch()
does not catch the error
Sure, because there is no Error then. But catch
will catch whatever you pass to reject
, which is the value 1
, which does get logged correctly.
这篇关于为什么.catch()不会在异步函数的循环内捕获Promise构造函数中的reject(),除非传递错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!