How can I tell if an object is an Angular $q promise?

后端 未结 3 1983
天命终不由人
天命终不由人 2021-01-18 18:03

I have a pre-existing non-Angular API library in my project. It has a .request method which returns jQuery.Deferred promises. I created a simple Angular service

相关标签:
3条回答
  • 2021-01-18 18:11

    Generally the better approach is to cast whatever object you do have into an Angular promise.

    The concept of assimilating thenables is part of the Promises/A+ specification. Most promise libraries have a way to do this. It's what allows for awesome interop between promise implementations and a uniform API.

    For this, $q uses .when :

    Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted.

    It uses the concept of thenables to convert a 'non trusted' promise into a $q promise.

    So all you have to do is

    var p = $q.when(value); // p is now always a $q promise
                            // if it already was one - no harm
    
    0 讨论(0)
  • 2021-01-18 18:23

    My current solution is to use instanceof:

    var AngularPromise = $q.resolve().constructor;
    
    console.log($q.resolve() instanceof AngularPromise);  // true
    

    This guarantees to return true iff the object is indeed an Angular Promise.

    Demo: https://jsfiddle.net/DerekL/cmzp7ovj/

    0 讨论(0)
  • 2021-01-18 18:35

    As a partial solution, adequate and appropriate for the given example, we can easily distinguish between jQuery.Deferred promises and Angular promises by checking for the presence of specific methods. For instance, Angular's $q promises have the method catch to handle errors, while jQuery.Deferred's promises have the method fail.

    function promiseIsAngularOrJQuery(promise) {
        if (typeof promise.fail === 'function') {
            return 'jquery';
        } else if (typeof promise.catch === 'function') {
            return 'angular';
        } else {
            throw new Error("this can't be either type of promise!");
        }
    }
    

    However, using this technique to distinguish between more different types of promises, or between promises and non-promises, could get very messy. Different implementations often use use many of the same method names. It could probably be made to work, but I'm not going down that road.


    There is an alternative that should be able to reliably identify $q promises under reasonable conditions: operating on trusted objects in a non-hostile environment, with only a single version of Angular in use. However, some might see it as too "hacky" for serious use.

    If you convert a function to a string using the String() function, the result is the source code for that function. We just need to use this to compare the .then method on a potential promise object to the .then method of a known $q promise object:

    function isAngularPromise(value) {
        if (typeof value.then !== 'function') {
            return false;
        }
        var promiseThenSrc = String($q.defer().promise.then);
        var valueThenSrc = String(value.then);
        return promiseThenSrc === valueThenSrc;
    }
    
    0 讨论(0)
提交回复
热议问题