微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-Promise.all中的Promise.all中的诺言遭到多次拒绝,究竟发生了什么?

Promise.all([iterable])是全有还是全无,意味着当可迭代对象中的每个promise都解决时,它返回的promise就会解决,或者一旦其中一个promise拒绝,它就会被拒绝,原因是第一个promise拒绝(doc) ).

但是,如果可重复拒绝的多重承诺会怎样?

在VSCode中,我尝试了以下示例,并故意使foo()和bar()承诺均告失败.当我在VSCode中调试时,我在* catch(err => Promise.reject(‘错误查询bar()’))*上收到一条错误消息,说发生了异常,我不明白为什么.

我认为这是因为当我调用Promise.reject时,Promise.all已经从foo函数中收到了拒绝,该拒绝也失败了,但是尚不清楚发生了什么.

如果我在调试选项中禁用了“未捕获的异常”断点,则该异常不再显示.

这里到底发生了什么?

function foo() {
  return pool.query('insert_test into test (value) values (20)')
    .then(() => client.query('insert into test (value) values (21)'))
    .catch(err => Promise.reject('error query bar()'))
}

function bar() {
  return pool.query('insert_test into test (value) values (20)')
    .then(() => client.query('insert into test (value) values (21)'))
    .catch(err => Promise.reject('error query bar()'))
 }

 Promise.all([foo(), bar()])
   .then(results => {
     console.log(results)
   })
   .catch(err => {
     console.log(err)
   });

这是我启用了“未捕获的异常”后看到的屏幕截图.

enter image description here


解决方法:

But what happens if multiple promises of the iterable reject?

一个拒绝获胜,并有理由拒绝Promise.all承诺.如果Promise.all访问它们时,承诺已被拒绝,则“最早”表示“最早发生”,迭代顺序很重要.

随后的拒绝将被忽略.

If I disable the “Uncaught Exceptions” breakpoint in the debugging options, the exception doesn’t show up anymore.

这很奇怪.忽略的拒绝不应导致未处理的拒绝(或未捕获的异常).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐