-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Request: angular.isPromise helper since $q.prototype !== Promise #14209
Comments
It can't inherit from promise, because not all supported browsers feature Promise. As for a helper, we don't really want to expose more public helper functions - only if we have internal use for them. So I would say the best idea is to write your own helper fn. |
BTW, in recent versions (I don't remember exaclty since when), the promises created with .factory('isPromise', function isPromiseFactory($q, $window) {
return function isPromise(val) {
return (val instanceof $q) || ($window.Promise && (val instanceof $window.Promise));
};
}) |
#13545 looks like it landed in 1.5. Promise detection is an issue for external code trying to interop with angular. Just getting access to $q externally is difficult and hackish. The scenario where this is biting me is a jquery widget with an event callback that I'm wiring in angular and returning a router navigation promise. I'm trying to minimize the extent to which the widget knows it's talking to angular, but the lack of a standard promise means |
No, we can't arbitrarily add things to the prototype chain. This might break any time. FWIW, our internal function isPromiseLike(obj) {
return obj && isFunction(obj.then);
} which in native JS translates to: function isPromiseLike(obj) {
return obj && (typeof obj.then === 'function');
} |
This sounds like this isn't likely to be added given comments here - would it be worthwhile to close this issue? |
In angular 1.5.0, if I want to test a value to see if it is a promise, I can't just say
x instanceof Promise
because the promises created by $q do not inherit from the native Promise implementation. I don't know if it would be practical for $q to extend Promise. If not, it would be useful if angular provided anangular.isPromise(p:any): boolean
utility function that would return true for both $q and native promises.The text was updated successfully, but these errors were encountered: