How does Angular $q.when work?

AngularjsDeferredQ

Angularjs Problem Overview


Can some one explain me how does $q.when work in AngularJS? I'm trying to analyse how $http work and found this:

var promise = $q.when(config);

And here is config object from Chrome console:

Object {transformRequest: Array[1], transformResponse: Array[1], cache: Object, method: "GET", url: "/schedule/month_index.html"…}
cache: Object
headers: Object
method: "GET"
transformRequest: Array[1]
transformResponse: Array[1]
url: "/schedule/month_index.html"
__proto__: Object

What happens next? How this object get's resolved or rejected?

Angularjs Solutions


Solution 1 - Angularjs

Calling $q.when takes a promise or any other type, if it is not a promise then it will wrap it in a promise and call resolve. If you pass a value to it then it is never going to be rejected.

From the docs:

> 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSETView Question on Stackoverflow
Solution 1 - AngularjsDerek EkinsView Answer on Stackoverflow