为什么.catch()不会在异步函数的循环内捕获Promise构造函数中的reject(),除非传递错误? [英] Why does .catch() not catch reject() within Promise constructor within loop at an async function unless Error is passed?

查看:188
本文介绍了为什么.catch()不会在异步函数的循环内捕获Promise构造函数中的reject(),除非传递错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定



 (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 值可在

$ b $下找到b

  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) ));  



问题:


  1. 为什么 reject()调用不会传播到 .catch()何时错误()未在承诺中明确传递给 reject() / code> async 函数循环中的构造函数?


  2. 为什么只有一个单个 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:

  1. Why does reject() call not propagate to .catch() when Error() is not explicitly passed to reject() within Promise constructor within a loop at an async function?

  2. Why is only a single Promise value returned to .then() though an array is returned from the async function when one of the Promise objects iterated at a loop reject() function is called within Promise constructor resolver function?

解决方案

If we […] do not explicitly pass Error() to reject() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆