I came upon this script from Google search at jQuery Deferred and Promise for sequential execution of synchronous and asynchronous funcitons .
This may solve my issue on executing the script in sequiental order. But the problem I have with then() function is I have no way of knowing how many time can I call this function. I can call the function a() 3 times, next time, I can call it 20 times, etc. So, that won't work with then(). then() function doesn't allow the array stack. So, what do you people do to to make this concept work?
fucntion a(counter)
{
$deferred = $.Deferred();
//Run some scripts here...
alert(counter);
$deferred.resolve();
return $deferred;
}
function Test()
{
var d = $.Deferred(),
p=d.promise();
p.then(a(0)).then(a(1)).then(a(2));
d.resolve();
return d;
}
Test().done(function(){ alert('done'); }).fail(function() { alert('fail'); });