Promises are POST PROCESSING LOGICS which you want to execute after
some operation / action is completed. While deferred helps to control
how and when those promise logics will execute.
We can think about promises as “WHAT” we want to fire after an operation is completed while deferred controls “WHEN” and “HOW” those promises will execute.
For example after an operation is complete you want to a send a mail, log in to log file and so on. So these operations you will define using promise. And these promise logics will be controlled by deferred.
We are thankful to www.stepbystepschools.net for the above image.
So once some action completes deferred gives a signal “Resolve”,
“Reject” or “Notify” and depending on what kind of signal is sent the
appropriate promise logic chain fires.
“$q” is the angular service which provides promises and deferred functionality.
Using promises, deferred and “q” service is a 4 step process:-
Below is the angular code for the above 4 steps.
We can think about promises as “WHAT” we want to fire after an operation is completed while deferred controls “WHEN” and “HOW” those promises will execute.
For example after an operation is complete you want to a send a mail, log in to log file and so on. So these operations you will define using promise. And these promise logics will be controlled by deferred.

“$q” is the angular service which provides promises and deferred functionality.
Using promises, deferred and “q” service is a 4 step process:-
- Step 1:- Get the “q” service injected from Angular.
- Step 2 :- Get deferred object from “q” service object.
- Step 3 :- Get Promise object from deferred object.
- Step 4 :- Add logics to the promise object.

// Step 1 :- Get the "q" service
function SomeClass($scope,$q) {
// Step 2 :- get deferred from "q" service
var defer = $q.defer();
// step 3:- get promise from defer
var promise = defer.promise;
// step 4 :- add success and failure logics to promise object
promise.then(function () {
alert("Logic1 success");
}, function () {
alert("Logic 1 failure");
});
promise.then(function () {
alert("Logic 2 success");
}, function () {
alert("Logic 2 failure");
});
}
So now depending on situations you can signal your promise logics via
deferred to either fire the success events or the failure events.// This will execute success logics of promise
defer.resolve();
// This will execute failure logics of promise
defer.reject();
HI, pretty article,interesting to read this blog , keep more postangular training institute in chennai
ReplyDeletehere you go for more AngularJs Interview Questions
ReplyDeleteOnline Assignment Help Tasmania - Australia Best Tutor is responsible for providing an excellent range of Online Assignment help Tasmania to the students pursuing different subjects as part of their studies.
ReplyDeleteRead More : http://prsync.com/australia-best-tutor/get-good-grades-by-using-the-online-assignment-help-tasmania-2589126
Very informative blog. Thnaks for sharing.
ReplyDeleteAngular js online training
Angular js online course
Angular js Online Training in Hyderabad
Angular js Online Training in Bangalore
Angular js Online Training in Chennai
Really Good blog post.provided a helpful information.I hope that you will post more updates like this AngularJS5 Online Training Hyderabad
ReplyDeleteAngularJS5 Online Course Hyderabad
I always enjoy reading quality articles by an individual who is obviously knowledgeable on their chosen subject. Ill be watching this post with much interest. Keep up the great work, I will be back
ReplyDeleteData Science Course in Indira nagar
Data Science Course in btm layout
Python course in Kalyan nagar
Data Science course in Indira nagar
Data Science Course in Marathahalli
Data Science Course in BTM Layout
I recently came across your blog and have been reading along. I thought I would leave my first comment.
ReplyDeleteJava training in Chennai | Java training institute in Chennai | Java course in Chennai
Java training in Bangalore | Java training institute in Bangalore | Java course in Bangalore
Java online training | Java Certification Online course-Gangboard
Java training in Pune
Really Good blog post provided helpful information. I hope that you will post more updates like this...
ReplyDeleteCore PHP Training Institute in Noida
C C++ Training Institutes in Noida
Excellent post, it will be definitely helpful for many people. Keep posting more like this.
ReplyDeleteDot Net Training Institute in Noida
Oracle Training Institutes in Noida
Really Good blog post provided helpful information. I hope that you will post more updates like this...
ReplyDeleteAngular JS Training in Noida
Dot Net Training Institute in Noida