如何返回许多 Promise 并在执行其他操作之前等待所有 Promise
我有一个循环,它调用一个异步执行操作的方法。这个循环可以多次调用该方法。在这个循环之后,我有另一个循环,仅当所有异步工作完成时才需要执行。
所以这说明了我想要的:
for (i = 0; i < 5; i++) {
doSomeAsyncStuff();
}
for (i = 0; i < 5; i++) {
doSomeStuffOnlyWhenTheAsyncStuffIsFinish();
}
我对承诺不太熟悉,所以有人可以帮助我实现这一目标吗?
这就是我的 doSomeAsyncStuff()
的行为方式:
function doSomeAsyncStuff() {
var editor = generateCKEditor();
editor.on('instanceReady', function(evt) {
doSomeStuff();
// There should be the resolve() of the promises I think.
})
}
也许我必须做这样的事情:
function doSomeAsyncStuff() {
var editor = generateCKEditor();
return new Promise(function(resolve,refuse) {
editor.on('instanceReady', function(evt) {
doSomeStuff();
resolve(true);
});
});
}
但我不确定语法。
I have a loop which calls a method that does stuff asynchronously. This loop can call the method many times. After this loop, I have another loop that needs to be executed only when all the asynchronous stuff is done.
So this illustrates what I want:
for (i = 0; i < 5; i++) {
doSomeAsyncStuff();
}
for (i = 0; i < 5; i++) {
doSomeStuffOnlyWhenTheAsyncStuffIsFinish();
}
I'm not very familiar with promises, so could anyone help me to achieve this?
This is how my doSomeAsyncStuff()
behaves:
function doSomeAsyncStuff() {
var editor = generateCKEditor();
editor.on('instanceReady', function(evt) {
doSomeStuff();
// There should be the resolve() of the promises I think.
})
}
Maybe I have to do something like this:
function doSomeAsyncStuff() {
var editor = generateCKEditor();
return new Promise(function(resolve,refuse) {
editor.on('instanceReady', function(evt) {
doSomeStuff();
resolve(true);
});
});
}
But I'm not sure of the syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
Promise.all
(规范,< a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all" rel="nofollow noreferrer">MDN):它接受一堆单独的承诺,并返回一个承诺,当您提供的所有承诺都得到履行时,该承诺得到履行,或者当其中任何一个承诺被拒绝时,该承诺被拒绝。履行数组中的结果与输入数组中的 Promise 的顺序相同。因此,如果您让
doSomeAsyncStuff
返回一个 Promise,那么:MDN 有一篇关于 Promise 的文章 此处。我还在我的书 JavaScript:新玩具 的第 8 章中详细介绍了 promsies,如果您有兴趣,可以在我的个人资料中找到链接。
这是一个例子:
示例输出(由于
Math.random
,首先完成的内容可能会有所不同,但“全部完成”行将位于最后):再次注意,fulfillment 数组的内容位于输入数组中承诺的顺序(不是承诺的履行顺序)。
You can use
Promise.all
(spec, MDN) for that: It accepts a bunch of individual promises and gives you back a single promise that is fulfilled when all of the ones you gave it are fulfilled, or rejected when any of them is rejected. The results in the fulfillment array are in the same order as the promises in the input array.So if you make
doSomeAsyncStuff
return a promise, then:MDN has an article on promises here. I also cover promsies in detail in Chapter 8 of my book JavaScript: The New Toys, links in my profile if you're interested.
Here's an example:
Sample output (because of the
Math.random
, what finishes first may vary, but the "All done" line will be at the end):Note, again, that the contents of the fulfillment array are in the order of the promises in the input array (not the order the promises were fulfilled in).
可重用函数非常适合此模式:
OP 示例:
相关模式是迭代数组并对每个项目执行异步操作:
示例:
A reusable function works nicely for this pattern:
OP example:
A related pattern, is iterating over an array and performing an async operation on each item:
Example:
这是我为自己编写的代码,以便理解此处所述的答案。我在 for 循环中有猫鼬查询,因此我将
asyncFunction
放在此处来代替它。希望它对任何人都有帮助。您可以在节点或许多 Javascript 运行时中的任何一个中运行此脚本。Here is code that I wrote for myself in order to understand the answers stated here. I have mongoose queries in a for loop, so I put here the
asyncFunction
to take its place. Hope it helps anyone. You can run this script in node or any of many Javascript runtimes.如果您想多次执行相同的操作,这里有一个优雅的解决方案:
这将创建一个包含 10 个项目的数组,用零填充它,然后将其映射到一个 Promise 数组。
Here's an elegant solution for you if you want to do the same thing multiple times:
This creates an array with 10 items, fills it with zeros and then maps it to an array of promises.