[英]Node.js - promises in a loop
I have the this problem to handle nested promises and loops: 我有这个问题来处理嵌套的承诺和循环:
firstPromise().then(function(resultOfFirstPromise){
var promiseArray = [];
for(var i = 0; i < 10; i++){
var ret = secondoPromise();
promiseArray.push(ret);
}
return Promise.all(promiseArray);
}, function(error){
console.log("firstPromiseError");
}).then(function(resultOfSecondPromise){
console.log(i); //but obviously i = 10
}, function(error){
console.log("secondPromiseError");
});
What I want to do is to have the loop index value i
in every .then
of each secondPromise: the output of my code should by somthing like this: 我想要做的是有循环的索引值
i
在每一个.then
每个secondPromise的:我的代码的输出应该通过这样的财产以后:
0 1 2 3 4 5 6 7 8 9 10. 0 1 2 3 4 5 6 7 8 9 10。
What you are trying to achieve is not possible with Promise
. Promise
无法实现您想要实现的目标。 In the chain of .then()
, one .then()
is run only after all of its previous .then()
are done. 在
.then()
链中,一个.then()
仅在其所有先前的.then()
完成之后才运行。 Meaning, if a .then()
has finished running, it will never run again. 这意味着,如果
.then()
已完成运行,它将永远不会再次运行。
I don't know your use case but what you are trying to do is achievable using Rxjs/Observables
我不知道您的用例,但是使用
Rxjs/Observables
可以实现
let arr = [1, 2, 3, 4, 5];
Rx.Observable.from(arr)
.do((v) => {
/*do some thing*/
console.log(v)
})
.do((v) => {
/*do another things*/
console.log(v)
});
In above code callback inside each do()
will run for each element of the arr
. 在上面的代码中,每个
do()
内部的回调将为arr
每个元素运行。
Output
: Output
:
1
1
2
2
3
3
4
4
5
5
Just chain the then
calls to secondoRomise()
, and also use let
in the loop: 只需将
then
调用链接到secondoRomise()
,然后在循环中使用let
:
firstPromise().then(function(resultOfFirstPromise){
var promiseArray = [];
for(let i = 0; i < 10; i++){
var ret = secondoPromise();
promiseArray.push(ret);
ret.then(result => console.log("Promise No." + i + " returned " + result);
}
return Promise.all(promiseArray);
}).then(() => console.log("all done!"));
This is just a scope problem because when you create a new function, it creates a new scope so you can't access one scope from another, it acts sort of like creating a new class in Javascript. 这只是一个范围问题,因为当您创建一个新函数时,它会创建一个新范围,因此您无法从另一个范围访问一个范围,其行为类似于在Javascript中创建一个新类。 This is the exact same reason when you create a function you often have to create a variable to reference this (eg const self = this).
这与创建函数时的原因完全相同,通常必须创建一个变量来引用此函数(例如const self = this)。 You can get around this using the ES6 arrow functions, since it doesn't create a new class/scope when you use them.
您可以使用ES6箭头功能解决此问题,因为使用它们时它不会创建新的类/作用域。
firstPromise().then( (resultOfFirstPromise) => {
var promiseArray = [];
for(var i = 0; i < 10; i++){
var ret = secondoPromise();
promiseArray.push(ret);
}
return Promise.all(promiseArray);
}, (error) => {
console.log("firstPromiseError");
}).then( (resultOfSecondPromise) => {
console.log(i); //but obviously i = 10
}, (error) => {
console.log("secondPromiseError");
});
Something like that, you may need to finagle a little bit. 像这样的事情,您可能需要稍作调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.