admin管理员组文章数量:1289543
I have a script like:
var a = [{'a': 1},{'b': 2}]
var allPromises = new Array(a.length)
for(var i in a) {
allPromises[i] = Promise.resolve().then(response => {
console.log(i)
console.log(a[i])
// Do somethig on every loop with key and value
return i
})
}
Promise.all(allPromises).then(response => console.log(response))
Here in my for loop
it only gives me last index and value of last index while I want the value on every loop and perform some action with key and value.. But I am getting the last key and value only..
I tried getting the value on Promise.all
's response but didnt work.
How can I get the index of my array on allPromises
's response ?
I can do it by makeing a counter. But when I again call the function the counter is reset so I donnt want to use the counter.
Is there anyway I can get index on every loop for promise ?
I have a script like:
var a = [{'a': 1},{'b': 2}]
var allPromises = new Array(a.length)
for(var i in a) {
allPromises[i] = Promise.resolve().then(response => {
console.log(i)
console.log(a[i])
// Do somethig on every loop with key and value
return i
})
}
Promise.all(allPromises).then(response => console.log(response))
Here in my for loop
it only gives me last index and value of last index while I want the value on every loop and perform some action with key and value.. But I am getting the last key and value only..
I tried getting the value on Promise.all
's response but didnt work.
How can I get the index of my array on allPromises
's response ?
I can do it by makeing a counter. But when I again call the function the counter is reset so I donnt want to use the counter.
Is there anyway I can get index on every loop for promise ?
Share Improve this question asked Jul 27, 2016 at 7:00 gamergamer 5,86314 gold badges59 silver badges92 bronze badges 3-
1
Using
let
instead ofvar
would also work with the way the for loop is written becauselet
works with block scoping, not only function scoping. But I would also prefer the solution by @jfriend00 because in my opinion theArray.map
/Array.forEach
is better readable. – Andreas Jägle Commented Jul 27, 2016 at 7:16 -
1
You already have some good answers, so you don't need another :) But to add one more option to the mix: since you already use resolve, you can pass
i
into it, so it bees the parameter insidethen
(resolve
) and use that parameter instead of the outeri
variable: jsfiddle/vscbgwpm – Me.Name Commented Jul 27, 2016 at 7:41 -
1
Using
var
is a typical JS scoping ambush here. You should uselet
instead ofvar
. Or @Me.Name's idea is also pretty valid and good. – Redu Commented Jul 27, 2016 at 7:51
2 Answers
Reset to default 9Your i
variable in the .then()
handler inside your for
loop isn't what you think it is. Your for
loop has already run to pletion before any of your .then()
handlers get called (since they always run asynchronously on a future tick). Thus, you only think you're seeing the last promise, but actually all the promises are working fine, it's just that they are all returning the last value of i
.
You can fix it by using .forEach()
to iterate your array because it uniquely captures each value of i
.
var a = [{'a': 1},{'b': 2}]
var allPromises = new Array(a.length);
a.forEach(function(item, i) {
allPromises[i] = Promise.resolve().then(response => {
console.log(i)
console.log(a[i])
// Do somethig on every loop with key and value
return i
})
});
Promise.all(allPromises).then(response => console.log(response))
Or, since you're generating an array as a result, you could use .map()
:
var a = [{'a': 1},{'b': 2}]
var allPromises = a.map(function(item, i) {
return Promise.resolve().then(response => {
console.log(i)
console.log(a[i])
// Do somethig on every loop with key and value
return i
})
});
Promise.all(allPromises).then(response => console.log(response))
I think the promise all is cool, but it's a pity to told you that is you are wrong. this is a promise all demo:
var p = [];
p[0] = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "one");
});
p[1] = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "two");
});
p[2] = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, "three");
});
p[3]= new Promise((resolve, reject) => {
setTimeout(resolve, 3000, "four");
});
p[4] = new Promise((resolve, reject) => {
setTimeout(resolve, 4000, "done");
});
Promise.all(p).then(value => {
console.log(value);
});
so I think your code is look like this:
var arr = [{ a: 1 }, { b: 2 }];
var p = [];
arr.forEach((val, err) => {
var promiseFunction = new Promise((resolve, reject) => {
resolve(val);
});
p.push(promiseFunction);
})
Promise.all(p).then(value => {
console.log(value);
});
what do you think?
本文标签: javascript Promiseall returns last promise onlyStack Overflow
版权声明:本文标题:javascript Promise.all returns last promise only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741421357a2377823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论