javascript - How to execute common code after a Dojo Deferred object is resolved or rejected? - Stack Overflow

I have a question about dojoDeferred.I'll start with the question, then go into more detail abo

I have a question about dojo/Deferred. I'll start with the question, then go into more detail about what I'm doing:

Is there a way to execute the same lines of code regardless of the oute of the deferred, sort of like a finally block in a try...catch statement? From what I've read, it doesn't seem like there is, but maybe I'm understanding the documentation wrong and wanted to verify that with the SO munity.

Here's what I'm doing:

In Dojo 1.9 (also works in 1.8), I instantiate a dojox.widget.Standby (a loading overlay) for a ContentPane before loading some data. Once the deferred call has pleted, I want to hide my overlay as shown below:

standby = new Standby({
    ... // standby props
});
this.addChild(standby);
standby.show();

queryResults = grid.store.query({
    ... // query props
});
queryResults.then(function (results) {
    if (results) {
        ... // do something
    }

    standby.hide();
}, function (error) {
    ... // handle error

   standby.hide();
});

This works fine; however, presumably, I could have some process to be implement after the deferred pletes that takes up several lines of code instead of just a single line and I wouldn't want to duplicate those lines of code. An alternative would be to create a private function and just call it with a one-liner in each block, but if there's a better way, I'd rather take that route.

Thanks in advance!

I have a question about dojo/Deferred. I'll start with the question, then go into more detail about what I'm doing:

Is there a way to execute the same lines of code regardless of the oute of the deferred, sort of like a finally block in a try...catch statement? From what I've read, it doesn't seem like there is, but maybe I'm understanding the documentation wrong and wanted to verify that with the SO munity.

Here's what I'm doing:

In Dojo 1.9 (also works in 1.8), I instantiate a dojox.widget.Standby (a loading overlay) for a ContentPane before loading some data. Once the deferred call has pleted, I want to hide my overlay as shown below:

standby = new Standby({
    ... // standby props
});
this.addChild(standby);
standby.show();

queryResults = grid.store.query({
    ... // query props
});
queryResults.then(function (results) {
    if (results) {
        ... // do something
    }

    standby.hide();
}, function (error) {
    ... // handle error

   standby.hide();
});

This works fine; however, presumably, I could have some process to be implement after the deferred pletes that takes up several lines of code instead of just a single line and I wouldn't want to duplicate those lines of code. An alternative would be to create a private function and just call it with a one-liner in each block, but if there's a better way, I'd rather take that route.

Thanks in advance!

Share Improve this question edited Jun 27, 2013 at 17:48 Default 16.5k3 gold badges28 silver badges38 bronze badges asked Jun 27, 2013 at 14:54 DavidDavid 3934 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You can use the always method of the Promises API to execute a function regardless of whether the underlying Deferred succeeds or fails.

queryResult
   .then(onSuccess, onFailure)
   .always(function() {
      standby.hide();
   });

This is a good question. A dojo/Deferred object will return another Deferred object when Deferred#then is called. This allows you to chain a differed with multiple callbacks that are fired in a serial order. Therefore, I believe you can do something like this:

queryResults.then(function (results) {
   if (results) {
       ... // do something
   }
}, function (error) {
    ... // handle error
}).then(function(data){
    // This will be fired with data returned from the previous callback.
    standby.hide();
});

You can see this example fiddle that illustrates a similar, albeit simple, use case where regardless of if the Deferred is rejected or resolved, the callback to the second Deferred#then is fired after the initial error/success callback.

var deferred = new Deferred(); 
deferred.promise.always( function() { alert('ciao'); } );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469868a4629084.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信