Monday, January 18, 2016

Explain $q service, deferred and promises?

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:-
  • 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.
Below is the angular code for the above 4 steps.
// 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();

10 comments:

  1. HI, pretty article,interesting to read this blog , keep more postangular training institute in chennai

    ReplyDelete
  2. Online 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.

    Read More : http://prsync.com/australia-best-tutor/get-good-grades-by-using-the-online-assignment-help-tasmania-2589126

    ReplyDelete
  3. Really Good blog post.provided a helpful information.I hope that you will post more updates like this AngularJS5 Online Training Hyderabad
    AngularJS5 Online Course Hyderabad

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

    Data 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

    ReplyDelete
  5. Really Good blog post provided helpful information. I hope that you will post more updates like this...
    Core PHP Training Institute in Noida
    C C++ Training Institutes in Noida

    ReplyDelete
  6. Excellent post, it will be definitely helpful for many people. Keep posting more like this.
    Dot Net Training Institute in Noida
    Oracle Training Institutes in Noida

    ReplyDelete
  7. Really Good blog post provided helpful information. I hope that you will post more updates like this...
    Angular JS Training in Noida
    Dot Net Training Institute in Noida

    ReplyDelete