Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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.

like image 788
Sebastien F. Avatar asked Dec 21 '13 21:12

Sebastien F.


People also ask

What is a promise AngularJS?

Promises in AngularJS are provided by the built-in $q service. They provide a way to execute asynchronous functions in series by registering them with a promise object. {info} Promises have made their way into native JavaScript as part of the ES6 specification.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.

What is deferred and promise in AngularJS?

defer() to create a Promise. A Promise is a function that returns a single value or error in the future. So whenever you have some asynchronous process that should return a value or an error, you can use $q. defer() to create a new Promise.


1 Answers

You can use $q.when to wrap the object as a promise (whether it is or not). Then, you can be sure that you are always dealing with a promise. This should simplify the code that then handles the result.

Documentation for $q.when is here with $q.

like image 174
Davin Tryon Avatar answered Sep 18 '22 15:09

Davin Tryon