Promise.any() 和 Promise.first() 有什么区别? [英] What is the difference between Promise.any() and Promise.first()?

查看:80
本文介绍了Promise.any() 和 Promise.first() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们看起来几乎相同,但 有不同的描述,例如你不知道的 JS 一书.

They look almost identical but have different descriptions such as on the book You Don't Know JS.

难道他们都试图在不关心任何拒绝的情况下获得第一个已解决的答案(或收到第一个成功解决的通知)?

Don't they both try to get the first resolved answer (or get notified of the first successful resolve), without caring about any rejection?

推荐答案

您参考的文章提供了 .first() 的实现,并且还提供了 .any().first():

The article you refer to provides an implementation of .first() and it also provides definitions of both .any() and .first():

  • any([ .. ]) 类似于 all([ .. ]),但它忽略任何拒绝,因此只需要满足一个而不是所有的
  • first([ .. ]) 就像与 any([ .. ]) 的比赛,它忽略任何拒绝并尽快满足第一个 Promise 实现了.
  • any([ .. ]) is like all([ .. ]), but it ignores any rejections, so only one needs to fulfil instead of all of them.
  • first([ .. ]) is a like a race with any([ .. ]), which is that it ignores any rejections and fulfils as soon as the first Promise fulfils.

any 的这个定义似乎与 目前处于 EcmaScript 阶段 3 的那个.any 的引用定义似乎在等待 all 承诺得到解决.返回的承诺只会在所有都被拒绝时被拒绝,但如果至少一个承诺被履行,将会履行——忽略拒绝.

This definition of any seems different from the one that is currently in stage 3 for EcmaScript. The quoted definition of any seems to await all promises to be settled. The returned promise will only be a rejection when all those were rejected, but will fulfil, if at least one promise fulfilled -- ignoring the rejections.

短语so only one need to meet而不是全部"有点令人困惑,但与.all()的比较不是关于什么时候结果确定,但在什么条件下它会实现.

The phrase "so only one needs to fulfil instead of all of them" is somewhat confusing, but the comparison with .all() is not about when the result settles, but in which conditions it will fulfil.

这篇关于Promise.any() 和 Promise.first() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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