0

I need some help with handling multiple promises at the same time.

I have a Promise foo(value) provided by a Library. I trigger this function on an array of value :

for (var i=0; i<this.array.length; i++) {
    var aPromise = Library.foo(this.array[i]);
    aPromise.then(function(result) {
        //I NEED TO KNOW HERE THE VALUE OF this.array[i] LINKED TO THE ACTUAL PROMISE RESULT
        alert(array[i]/*not working*/ + " return > " + result);
    }).catch[...];
}

I do not find a way to get a link between the which argument has been used to trigger the promise and the actual result of it. Is there a clean way to do it ?

  • The Promise is provided by an external Library that I cannot modify.
  • I tried to create my own Promise calling that Promise so I can build my own returned object with the inputs and output but I was unsuccessful.

Thanks !

DessDess
  • 787
  • 1
  • 6
  • 20
  • Have the promise return its input in addition to its result. –  Nov 04 '15 at 22:16
  • I though about that but the Promise is provided by an external Library that I cannot modify. I tried to create my own Promise calling that Promise so I can build my own returned object with the inputs and output but I was unsuccessful. – DessDess Nov 04 '15 at 22:18
  • 2
    `array[i]` would be working if your callback would close over `i`. – Bergi Nov 04 '15 at 22:23
  • That worked ! Thanks – DessDess Nov 04 '15 at 22:46

0 Answers0