admin管理员组文章数量:1426006
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 badges3 Answers
Reset to default 5You 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'); } );
版权声明:本文标题:javascript - How to execute common code after a Dojo Deferred object is resolved or rejected? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469867a2659700.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论