Any way to know if a variable is an angularjs promise?

Viewed 19801

I'm making a directive that takes a function as a scope parameter (scope: { method:'&theFunction' }). I need to know if the result returned by that method is an angular promise (if yes something will happen on resolution, otherwise it happens right away).

For now I'm testing if foo.then exists but I was wondering if there was a better way to do it.

4 Answers

The $q.when() answer seems like the best answer for most use cases, I used instanceof for mine.

    if(buttonData instanceof $q) {
        buttonData.then(function(actions) {
            $scope.buttonActions = actions;
        });
    } else {
        $scope.button = buttonData;
    }

Alternatively, the following IF worked as well, but I ended up going with the above solution.

if(Object.getPrototypeOf(buttonData) === $q.prototype) {

Related