Question

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;

}

//wait
setTimeout(function(){

  $("#content").html(JSON.stringify(jsObj));

},300);

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. I'm aware I can create custom deferred objects (jQuery), but altering the API to use this or any other method of promise objects seems unrealistic.

Était-ce utile?

La solution

Deferred objects are indeed the way to go here. You don't have to change the API, just use a promise library like Q that can wrap it. It would look something like this:

promiseMethodName = Q.denodeify(methodName);

promiseMethodName("arg1", jsObj).then(function(result) {
  jsObj["data"] = result;
  return result;
}).then(function(result) {
  return promiseMethodName("arg1", anotherObj);
}).then(function(result) {
  jsObj["data"] = result;
  return result;
}).then(function(result) {
  $("#content").html(JSON.stringify(jsObj));
});

If your API doesn't use node-style callbacks, you might need to implement your own version of denodeify, which can be tricky, but the rest will be the same.

Licencié sous: CC-BY-SA avec attribution
scroll top