如何归还许多诺言并等待所有承诺,然后再做其他事情
我有一个循环,该循环调用一种方法,该方法会异步地做东西。该循环可以多次调用该方法。在此循环之后,我只有在所有异步工作完成时才需要执行另一个循环。
因此,这说明了我想要的:
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
( spec ,< a href =“ https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/global_objects/promise/promise/promise/all” rel =“ nofollow noreferrer”> nofollow noreferrer“> mdn )一堆个人的承诺,并给您一个单一的承诺,当您给予的所有承诺都会实现,或者在任何人被拒绝时被拒绝。履行数组中的结果与输入数组中的承诺的顺序相同。因此,如果您制作
dosomeasyncstuff
返回诺言,则:mdn在Promises 在这里。我还详细介绍了我的书 javascript的第8章:新玩具,如果您有兴趣,我的个人资料中的链接。
这是一个例子:
示例输出(由于
Math.random
,首先完成的内容可能会有所不同,但是“全部完成”行将在结尾处):再次请注意,履行数组的内容在输入阵列中承诺的顺序(不是履行的顺序)。
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 Runtimes中的任何一个中运行此脚本。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个项目的阵列,将其填充零,然后将其映射到一系列承诺中。
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.