- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果使用非空数组调用 Promise.all 或 Promise.race,它们将返回一个待处理的 Promise:
console.log(Promise.all([1]));
// prints Promise {<pending>}
console.log(Promise.race([1]));
// returns Promise {<pending>}
如果使用空数组调用 Promise.race,它将返回一个待处理的 Promise:
console.log(Promise.race([]));
// prints Promise {<pending>}
但是如果使用空数组调用 Promise.all,它会返回一个已解决的 Promise:
console.log(Promise.all([]));
// prints Promise {<resolved>: Array(0)}
为什么Promise.all函数要这样设计?似乎没有充分的理由造成这种不一致,但也许我遗漏了一些东西。
最佳答案
来自EMCA Script specification for Promise.race()
:
<小时/>If the iterable argument is empty or if none of the promises in iterable ever settle then the pending promise returned by this method will never be settled
在这方面,Promise.all()
规范并不是那么容易遵循,但基本上当你向它传递一个空数组时,它会以他们所说的 remainingElementsCount
为 0
,使其立即解析。
当您向其传递一个值(如 Promise.all([1])
中的值时),它可能会将该值包装在 Promise.resolve()
中,然后跟踪.then()
处理程序将在下一个价格变动时解析,因此 console.log(Promise.all([1]))
显示它在下一个价格变动之前仍处于待处理状态.
从逻辑上讲,这是有一定道理的。 Promise.race()
应该解析为第一个要解析的 Promise 的值,但是如果你不传递任何东西,那么实际上就没有第一个解析的值。唯一的其他选项是拒绝或解析为“未定义”或抛出无效使用的异常。我不太清楚为什么设计师选择他们所做的结果与其他选择相比,但至少在规范中清楚地详细说明了这一点。
Promise.all()
可以很好地解析为空数组,这是传递空数组的逻辑结果。
Why was the Promise.all function designed like this?
要真正“了解”设计者的逻辑,您必须询问其中之一,或者参与过讨论,或者查找讨论逻辑的邮件列表讨论。
但是,有人可以提出这样的论点:如果您有一个可变长度的数组,您想要等待使用 Promise.all()
完成,那么无论数组有 20 个,该函数都应该起作用其中的项目或 0。对于 0
长度的数组,它只会在下一个刻度时立即解析,这既有用又一致,因为没有 promise 等待,并且有一个适合且一致的解析值(空数组)。
ES6主题讨论
这里有一个关于 Promise.race()
从 Unresolved 开发讨论的链接:https://github.com/domenic/promises-unwrapping/issues/75 。当然有人不同意当前的实现。
我个人的观点(在该主题的各种讨论中由其他一些人分享)是它应该抛出异常,因为它基本上是一个无效条件,并且从开发的 Angular 来看“快速失败且引人注目”比无限的 promise 。但是,显然有更多的人喜欢它本来的样子。
Bluebird docs建议使用他们的 Promise.any()
而不是 Promise.race()
,部分原因是它没有这种行为。
关于javascript - Promise.all([]) 返回已解决的 Promise,但 Promise.race([]) 返回待处理的 Promise。为什么它们不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49849679/
我经常发现这些术语被用在并发编程的上下文中。它们是相同的还是不同的? 最佳答案 不,它们不是同一件事。它们不是彼此的子集。它们也不是彼此的必要条件,也不是充分条件。 数据竞争的定义非常明确,因此,它的
我在测试我的项目时遇到了 DATA RACE 警告,想知道是否有人愿意帮助我破译这个问题。我过去从未尝试过测试 go 例程,我发现很难全神贯注于数据竞赛。 我在描述中提供了指向未解决问题的链接,并在问
起初,我知道代码有一些竞争条件,所以我使用“go build -race”命令来检查它,我想看看结果如何显示,当我第一次运行时,它显示了第一个结果如下,然后再次运行显示第二个,它有两个不同的结果,我不
我在考虑我的代码中的极端情况,我不知道如何在检查文件是否存在时避免问题,如果不存在,则创建一个具有该文件名的文件。代码大致如下所示: // 1 status = stat(filename); if
我在考虑我的代码中的极端情况,我不知道如何在检查文件是否存在时避免问题,如果不存在,则创建一个具有该文件名的文件。代码大致如下所示: // 1 status = stat(filename); if
我想知道是否存在插入查询上实际发生竞争条件问题的真实案例。所以我有一个包含以下字段的“用户”表: User: iduser | idcompany | name | email 我为此表使用复合主键(
我有一段代码(一个测试运行器)应该运行代码并返回结果,我想为每个测试用例设置一个时间限制所以我使用 Promise.race 但不幸的是它不起作用 const createTestCafe = req
我遇到了 an implementation JavaScript 中的 Promise.race() 方法,它按预期工作,但对我来说意义不大。 const race = (...promises)
我的测试代码如下,使用threading,count不是5,000,000,所以出现data race,但是使用gevent,count是5,000,000,没有data race。 难道gevent
据我所知,关于promise有两个选项: promise.all() promise.race() 好的,我知道 promise.all() 是做什么的。它并行运行 Promise,如果两者都成功解析
又是我和我的BlockingQueue...我根据this article重写了它和 this question .它发送了一些项目,然后因访问冲突而崩溃。这是代码: template bool D
所以我有两个Python3.2进程需要相互通信。大多数需要交流的信息都是标准词典。命名管道似乎是可行的方法,所以我制作了一个可以在两个进程中实例化的管道类。这个类实现了一个非常基本的协议(protoc
我有一个注册页面,它接收 token 并解析它们并在参数适用时登录用户。 在我检查 token 的时间到我从数据库中删除 token 的时间之间,另一个用户可以使用相同的 token 登录。有没有办法
我在 redis 中有一个散列,其中一个字段的值为字符串化数组,每当用户注册一个事件时, 从redis中获取这个字符串化数组 后台解析,将用户的用户名添加到数组中 将数组字符串化并存储回哈希 如果两个
我知道之前有人问过这个问题,但我仍然很困惑,如果可能的话,我想在开始编程之前避免任何问题。 我计划拥有一个在任何给定时间至少有 100 名活跃用户的内部网站。用户将发布一个项目(以 0 作为其值插入到
我在面试中被问到以下问题。给定以下代码,如果方法 add 和 doAction 被多个线程调用,我们如何在打印 toString 时得到 NullPointerException ?** public
为什么标志“-race”的结果与预期的不一样?它期望相同的结果:1000000 - 带有标志“-race”但没有这个 https://gist.github.com/romanitalian/f403
我正在尝试了解如何为以下代码修复此竞争条件。 sayHello := func() { fmt.Println("Hello from goroutine") } go sayHello()
我有一堆 goroutines 在循环中做一些事情。我希望能够暂停所有这些,运行一些任意代码,然后恢复它们。我尝试这样做的方式可能不是惯用的(我希望有更好的解决方案),但我不明白为什么它不起作用。 精
考虑一个实现 open()、read()、write()、close()、unlocked_ioctl() 和 mmap() 的 linux 设备驱动程序。 现在,假设多个(或相同的)进程同时打开同一
我是一名优秀的程序员,十分优秀!