Synchronizing when using Async APIs RRS feed

  • Question

  • My app is basically processing items from the network, and I need to use some Async api's to do the processing. My problem is that I can't process the next item until the first has been processed, and I can't find any way to synchronize on the results of the async calls. Is there any supported way of doing this?

    Monday, October 3, 2011 3:06 PM


  • If the async calls are wrapped as promises, then its pretty easy to coordinate the calls. Promises can be chained together, chaining the then clauses. if the then doesn't return a value, subsequent then's will chain when complete, or it can return a promise which will delay the next then.

    AsyncCall1(param1).then(function (result1) {
        //handle result
        // make the next async call
        return AsyncCall2(param2);
    }).then (function (result2) {
        //handle result of AsyncCall2
    }).then (function() {
        //waits until previous promise is complete
        return AsyncCall3(param3);
    }).then ...

    You can also coordinate a set of promises, by waiting until one of a set, or all of a set are complete.

    var p=[];
    for (var i=0; i<10; i++)
        p.push(AsyncCall(item[i]).then( function(result) {
            //be careful in here, if you use i, it will not have the value you expect
            // do something with result
    WinJS.Promise.any(p).then(function (){
        // this will be called when any of the promises in array p are complete
    WinJS.Promise.join(p).then(function (){
        // this will be called when all the promises in array p are complete


    • Marked as answer by Chris Sells Monday, October 10, 2011 3:31 PM
    Monday, October 3, 2011 5:24 PM