titlu titlu - 1 year ago 57
Javascript Question

Relation between .then and deferObj.resolve() of promise in angularjs

I am finding it hard to understand few concepts of promise in angularjs. I understand what is it and when to use it and that $q.defer creates a promise object which has methods like resolve(), reject() I know there functionalities but what I am not able to understand is when to use it with .then function in some cases we use it and in some cases we don't .

link1 link2 link3

I went through many links like above but still not getting , maybe I am understanding it all wrong . But anyone please explain when to put the .resolve method in .then function or if my understanding is totally wrong kindly correct me before downvoting as really did try to understand it on my own before posting the question over here.

cch cch
Answer Source

A Promise is an object that can be passed or returned and holds reference to the outcome of asynchronous behavior. In Angular they are created through the $q service.

$q.defer() creates an object that has special methods, among then resolve() and reject(). The resolve() method indicates successful completion of the execution and wraps the data for the promise to return later on. If something goes wrong the reject() method should be called, sending an error object or error message.

This function should have a caller, and this caller should capture a reference to the promise object.

Here is where the then() method can be called to obtain the results or handle the errors according to the case. Notice that the then() method takes 2 arguments, which are the functions that are going to be executed in case of success, or in case of error.

In link3 you can see one part of the code has the definition of the asynchronous function named getData and later there is the call to then() to obtain the results, this call is made by the caller of the asynchronous function getData.

In link2 Controller FatherCtrl is the one calling then() to process the response of the Service SonService. It may be confusing that there is also a call to the then() method in the SonService, but this is to obtain the result of the HTTP GET call, as the comment properly explains it:

// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default

In this example you only see the then() part because the get() method of the $http service is an asyncronous function that upon completion is making the resolve() or reject() method call accordingly.

From the official documentation:

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise.

And again, what is a promise? Check the beginning of this answer. The code for the methods of the $http service have been written already. When any of this methods are called, and they finish they call either resolve() or reject(). The developer has to write code accordingly to read this outcome. (Yes, this is the then() that you we were talking about in the comments ;) ).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download