vendredi 30 janvier 2015

How can I test if a function is returning a promise in Ember?

In Ember, I sometimes run into the situation where I need to check if a function is returning a promise. For example, if I have a route that is derived:



MyRoute = ParentRoute.extend({
beforeModel: function() {
this._super().then(function() {
// do something...
});
}
});


But although beforeModel can return a promise, it might not. In particular, if it's the default Ember.K implementation, then it doesn't. I'd rather not always be doing:



var res = this._super();
if (typeof res.then === "function") {
res.then(function() {
// do X
});
} else {
// do X
}


I assume there's a way to wrap something that one doesn't know if it's a thenable, and then chain regardless. But I couldn't find it in the documentation.


The above is undesirable because it's verbose, and requires having the code for X twice.


Thoughts?





Aucun commentaire:

Enregistrer un commentaire