Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted March 20th, 2016
AngularJS Promise

Promise is a method that waits for the right data to come in and then fulfills the command you gave it.

The promise method can be:
– Accepted: this means that the whole promise action is fulfilled and the command you gave it will succeed.
– Fulfilled but rejected: this means that the whole promise action is fulfilled but the promise action did not get the right data to succeed the command you gave it.
– Rejected: this means that the promise action did not even receive any data from the command you gave it and rejects the command to run.

If this was not clear enough I got a really easy story that explains the whole process of the promise action:

Weather prediction

There was a father with his son. This father is a fisherman and needs to know if the weather is good enough to go out fishing in piece, so he asked his son to go to the top of the mountain to see if the weather is good enough and his son “promised” him to climb up the mountain to predict the weather for his father.

The first day the kid climbed up the mountain and saw that the weather is going to be really good so he walked back to his father and told him that the weather looked clear and the father decided to go out fishing with his son. (Promise accepted)

The second day the kid climbed up the mountain again and saw that the weather did not look so good so he walked back to his father and him that the weather is going to be really bad so his father decided that he will stay home for this day. (promise received, rejected)

The third day the kid climbed up the mountain again and saw that it was really foggy so he could not see if it is going to be good weather or bad weather so we went back to his father and told him that he could not see anything up there so his father decided that he is not going to risk his life for fish and stayed home. (promise not received, rejected)

I thought that this was a clear way to understand what a promise does.

Promise explained in code

var makePromise = function() {
    Service.getAPI()
    .then(function(data) {
        if(data.APICall==='good') {
            apiCallReady();
            //You get all the data you need...
        }
        else {
            apiCallFailed();
            //You get data but not the data you want...
        },function(error) {
                apiCallFailed();
                //The function failed to retrieve the right data and rejected it...
            });
    };

about $q

$q = service in module ng (library)
This service helps you run functions on the same time and use their return values.

  • Then() When the promise was or will be resolved or denied , “then” call of the success or error call (at the same time as the result is available).
  • Finally() allows you to see the rejected value and the accepted value and does not modify the accepted value.

Posted by Geert Heemskerk

Leave a Reply

Your email address will not be published. Required fields are marked *