Async

Web browser asynchronous function utilities

Detail

The purpose of the Async library is to execute a group of asynchronous functions, with a single callback when they are complete.  marshal executes the functions all at the same time, and sequence executes them in the order defined.  Both of these functions return an async handle object that keeps track of the progress.  To execute a function when all the async functions are complete, call “then(somefunction)” on the returned handle object.

See

Note

both marshal and sequence recognize the returned async handle object, so these handle objects can be used to nest multiple sets of callbacks like so

var firstSet = Async.sequence(f1,f2,..fn);
var secondSet = Async.sequence(f1,f2,..fn);
//at this point both sets are executing their sequences in parallel
Async.marshal(
firstSet,
secondSet
).then(function(){
alert("both sets done!")
})
Summary
AsyncWeb browser asynchronous function utilities
Functions
marshalExecutes multiple async functions and then a single handler when done
sequenceExecutes multiple async functions, one after the other, and then a single handler when done

Functions

marshal

marshal:function()

Executes multiple async functions and then a single handler when done

Parameters

one or more async functions that accept a function as their first parameter, or an array of such functions.  You can use the returned handle from marshal or sequence

returns

handle object with a “then” function.  Call this with a function to execute when all async functions are complete

Example

    Async.marshal(
function(done){
window.setTimeout(function(){
console.log(1)
done()
},100)
},
function(done){
window.setTimeout(function(){
console.log(2)
done()
},200)
},
function(done){
window.setTimeout(function(){
console.log(3)
done()
},50)
}
).then(function(){
//this runs when all callbacks are done
console.log("all done!")
})

This outputs:
3
1
2
all done!

You can also delay the final handler until a later time by saving the handle object and calling then() when you are ready

<script>

var allCallbacks = Async.marshal(
function(done){
ajax1(done);
},
function(done){
ajax2(done);
},
function(done){
ajax3(done);
},
)
window.onload = function(){
//this will execute immediately if all the callbacks are done,
otherwise it will wait until they are all done before executing
allCallbacks.then(function(){
alert("Yippie! all done!")
})
}
</script>

sequence

sequence:function()

Executes multiple async functions, one after the other, and then a single handler when done

Parameters

one or more async functions that accept a function as their first parameter, or an array of such functions.  You can use the returned handle from marshal or sequence

returns

handle object with a “then” function.  Call this with a function to execute when all async functions are complete

Example

    Async.sequence(
function(done){
window.setTimeout(function(){
console.log(1)
done()
},100)
},
function(done){
window.setTimeout(function(){
console.log(2)
done()
},200)
},
function(done){
window.setTimeout(function(){
console.log(3)
done()
},50)
}
).then(function(){
//this runs when all callbacks are done
console.log("all done!")
})

This outputs:
1
2
3
all done!

You can also delay the final handler until a later time by saving the handle object and calling then() when you are ready

<script>

var allCallbacks = Async.sequence(
function(done){
ajax1(done);
},
function(done){
ajax2(done);
},
function(done){
ajax3(done);
},
)
window.onload = function(){
//this will execute immediately if all the callbacks are done,
otherwise it will wait until they are all done before executing
allCallbacks.then(function(){
alert("Yippie! all done!")
})
}
</script>
marshal:function()
Executes multiple async functions and then a single handler when done
sequence:function()
Executes multiple async functions, one after the other, and then a single handler when done