Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if a method returns an Promise

In angularjs, while testing a service, I want to check if the returned object is an Promise.

Right now I am doing the following -

 obj.testMethod()
        .should.be.instanceOf($q.defer());
like image 695
tusharmath Avatar asked Dec 01 '22 01:12

tusharmath


1 Answers

Testing if an object is a promise is simple:

return !!obj.then && typeof obj.then === 'function';

That's it. If an object has the then method, it's a promise.

It looks like angular's $q doesn't have anything to differentiate it from other kind of promises.

like image 148
Florian Margaine Avatar answered Dec 04 '22 08:12

Florian Margaine