Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Angular $q.when work?

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?

like image 702
SET Avatar asked May 27 '13 10:05

SET


People also ask

What is Q in angular?

$q is integrated with the $rootScope. Scope Scope model observation mechanism in AngularJS, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI. Q has many more features than $q, but that comes at a cost of bytes.

What is Q defer () in AngularJS?

Simply put you can use $q. 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.

What is angular IPromise?

IPromise , basically if you are using vs and as a suggestion when you hover over a particular method it will tell you what type it returns and you can derive the return type from that.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.


1 Answers

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.

like image 133
Derek Ekins Avatar answered Sep 19 '22 18:09

Derek Ekins