I need a bunch of functions to be called in strict order. It's also very important that the next function waits until the previous one has finished.
Right now I'm using chained callbacks:
callMe1(function(){
callMe2(function(){
callMe3(function(){
callMeFinal();
});
});
});
This works but seems to be a little ugly.
Any suggestions for a different approach?
If you use jQuery, then you can use
queue
to chain the functions.where
callMeX
should be of form:You might want to pass parameters to the functions, I do not believe you can at the time of this writing. However...
Wrapping your functions, arguments intact, with an anonymous function that plays along with
.queue
works too.Passing Arguments in Jquery.Queue()
Example on JSFiddle: http://jsfiddle.net/rS4y4/
You can implement a "stack" system:
Not sure if this would help you, but there is a great article on using deferreds in jQuery 1.5. It might clean up your chain a bit...
Also, my answer on Can somebody explain jQuery queue to me has some examples of using a queue for ensuring sequential calls.