Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 which wraps the .request method to transform its result into an Angular $q promise instead. It looks something like this:

var module = angular.module('example.api', []);

module.factory('api', function(
    $q,
    $window
) {
    function wrappedRequest() {
        var result = $window.API.request.apply($window.API, arguments);
        return $q.when(result);
    };

    return {
        request: wrappedRequest
    };
});

I would like to write a test which ensures that this service is functioning correctly. It will provide a mock $window with an API whose request method returns jQuery.Deferred promises. I need to ensure that the resulting objects are Angular $q promises.


How can I determine whether an object is an Angular $q promise?

For the example given in this question it would be sufficient to distinguish between jQuery.Deferred promises and Angular $q promises, but ideally we could identify Angular $q promises in general.

like image 430
Jeremy Avatar asked Mar 20 '14 21:03

Jeremy


2 Answers

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
like image 109
Benjamin Gruenbaum Avatar answered Sep 29 '22 08:09

Benjamin Gruenbaum


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;
}
like image 38
Jeremy Avatar answered Sep 29 '22 07:09

Jeremy