bigblind
bigblind

Reputation: 12867

How to deal with an object that might be a $q promise in angularjs?

In cases where a function may either return a value directly, or a $q.promise, what's the best way to handle that value? Is there a way to check weather an object is a $q.defer().promise, or is there an other way to deal with such an uncertainty?

Upvotes: 1

Views: 70

Answers (1)

Cuong Vo
Cuong Vo

Reputation: 4592

$q.when(value).then(...)

$q.when takes any value. If the value is not a promise, it's wrapped as a promise and resolved immediately, else, it's just returned. From Angular's q implementation:

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.

@param {*} value Value or a promise @returns {Promise} Returns a promise of the passed value or promise

Upvotes: 6

Related Questions