Answered by:
Looking for help when using multiple promises at one time

Question
-
Here is a basic example of what I am trying to accomplish. I have an array of things that I want to be updated using promises. The callback of each successful promise will update a value in the thing that started the promise.
var things = [...]; for(var i=0; i < things.length; i++) { getValuePromise(things[i]).then( function(value) { things[i].value = value; //this doesn't work of course because i is not in the scope } ); }
I know I could do this one promise at a time by storing which thing is being updated and then chaining the promises, but I was wondering if it is possible to shoot off all the promises at once and update the values as they complete. I just cant figure out a way tell each promise which thing to update.
This seems like something that should be easy to figure out, but I can't seem to wrap my head around it.
Tuesday, March 6, 2012 11:24 PM
Answers
-
Hi Jeff,
I found a way to get this to work. Here is an example where I have an array with 3 objects in it. I want to create 3 promises that return data specific to each object and then saves the data back within the object. By using a function to wrap each promise I can access the function parameters when the promise completes.
var objects = [ {name:"p1", data:"abc1"}, {name:"p2", data:"abc2"}, {name:"p3", data:"abc3"} ] for(var i=0; i<objects.length; i++) { updateData(objects[i].name, i); } function updateData(objectName, index) { getUpdatedDataAsync(objectName).then( //arbitrary promise that returns data based on parameter function(dataResult) { objects[index].data = dataResult; } ); }
Retrospectively, all I wanted was a way to cache variables locally for each promise so I can use them when the success function is called.
- Marked as answer by jfrtc Wednesday, March 7, 2012 2:31 AM
Wednesday, March 7, 2012 2:30 AM
All replies
-
If you use forEach on your array, you'd at least have a function closure that contains index as a parameter instead of using a for loop with an unbound i:
http://msdn.microsoft.com/en-us/library/bb397509.aspx
If you want to compose a number of promises together, you may find Promise.join useful:
http://msdn.microsoft.com/en-us/library/windows/apps/br211774.aspx
I'm not sure about more specific advice, because I don't quite understand your scenario. Can you share some more details? :)
Cheers,
-Jeff
Wednesday, March 7, 2012 12:06 AM -
Hi Jeff,
I found a way to get this to work. Here is an example where I have an array with 3 objects in it. I want to create 3 promises that return data specific to each object and then saves the data back within the object. By using a function to wrap each promise I can access the function parameters when the promise completes.
var objects = [ {name:"p1", data:"abc1"}, {name:"p2", data:"abc2"}, {name:"p3", data:"abc3"} ] for(var i=0; i<objects.length; i++) { updateData(objects[i].name, i); } function updateData(objectName, index) { getUpdatedDataAsync(objectName).then( //arbitrary promise that returns data based on parameter function(dataResult) { objects[index].data = dataResult; } ); }
Retrospectively, all I wanted was a way to cache variables locally for each promise so I can use them when the success function is called.
- Marked as answer by jfrtc Wednesday, March 7, 2012 2:31 AM
Wednesday, March 7, 2012 2:30 AM