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 of var would also work with the way the for loop is written because let works with block scoping, not only function scoping. But I would also prefer the solution by @jfriend00 because in my opinion the Array.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 inside then (resolve) and use that parameter instead of the outer i variable: jsfiddle/vscbgwpm – Me.Name Commented Jul 27, 2016 at 7:41
  • 1 Using var is a typical JS scoping ambush here. You should use let instead of var. Or @Me.Name's idea is also pretty valid and good. – Redu Commented Jul 27, 2016 at 7:51
Add a ment  | 

2 Answers 2

Reset to default 9

Your 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