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 outcome 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 community.
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 completed, 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 completes 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!
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With