1 of 3

How to handle asynchronous calls in an offline application

I'm using an offline application's javascript API and I'd like to know if I can use deferred objects to handle the callbacks. The API calls do not use HTTP, the calls are to and from the applications local database.

The only way I've been able to display information is by using 'setTimeout' on subsequent calls, which I know, is terrible! So I have a long list of callbacks and timeouts.

var jsObj = {};
var anotherObj = {};

//first async call
methodName("arg1", jsObj, callback);

function callback(result){

  jsObj["data"] = result;


//second async call
setTimeout(function(){methodName("arg1", anotherObj, callback2);}, 200);

function callback2(result){

  jsObj["data"] = result;





Is there anyway to refactor this? Any advice is appreciated. I've looked at the following post, but I'm not sure it would work.