Aug 31, 2015

JavaScript Promises

A Promise object represents a value that may not be available yet, but will be resolved at some point in future. It allows you to write asynchronous code in a more synchronous fashion. For example, if you use the Promise API to make an asynchronous call to a remote web service you will create a Promise object which represents the data that will be returned by the web service in future. 


the Promise object acts like a proxy to the actual data. Further, you can attach callbacks to the Promise object which will be called once the actual data is available.


The API

To get started, let’s examine the following code which creates a new Promise object.
              if (window.Promise) { // Check if the browser supports Promises
  var promise = new Promise(function(resolve, reject) {
    //asynchronous code goes here
  });
}
 We start by instantiating a new Promise object and passing it a callback function. The callback takes two arguments, resolve() and reject(), which are both functions. All your asynchronous code goes inside that callback. If everything is successful, the Promise is fulfilled by calling resolve(). In case of an error, reject() is called with an Error object. This indicates that the Promise is rejected.

Now let’s build something simple which shows how Promises are used. The following code makes an asynchronous request to a web service that returns a random joke in JSON format. Let’s examine how Promises are used here.


             if (window.Promise) {
  console.log('Promise found');

  var promise = new Promise(function(resolve, reject) {
    var request = new XMLHttpRequest();

    request.open('GET', 'http://api.icndb.com/jokes/random');
    request.onload = function() {
      if (request.status == 200) {
        resolve(request.response); // we got data here, so resolve the Promise
      } else {
        reject(Error(request.statusText)); // status is not 200 OK, so reject
      }
    };

    request.onerror = function() {
      reject(Error('Error fetching data.')); // error occurred, reject the  Promise
    };

    request.send(); //send the request
  });

  console.log('Asynchronous request made.');

  promise.then(function(data) {
    console.log('Got data! Promise fulfilled.');
    document.getElementsByTagName('body')[0].textContent = JSON.parse(data).value.joke;
  }, function(error) {
    console.log('Promise rejected.');
    console.log(error.message);
  });
} else {
  console.log('Promise not available');
}
 Promise can have three states:

pending (not fulfilled or rejected)
fulfilled
rejected

Chaining Promises
It is sometimes desirable to chain Promises together. For instance, you might have multiple asynchronous operations to be performed. When one operation gives you a data, you will start doing some other operation on that piece of data and so on. Promises can be chained together as demonstrated in the following example.

function getPromise(url) {
  // return a Promise here
  // send an async request to the url as a part of promise
  // after getting the result, resolve the promise with it
}

var promise = getPromise('some url here');

promise.then(function(result) {
  //we have our result here
  return getPromise(result); //return a promise here again
}).then(function(result) {
  //handle the final result
});
The tricky part is that when you return a simple value inside then(), the next then() is called with that return value. But if you return a Promise inside then(), the next then() waits on it and gets called when that Promise is settled.

Handling Errors
You already know the then() function takes two callbacks as arguments. The second one will be called if the Promise was rejected. But, we also have a catch() function which can be used to handle Promise rejection. Have a look at the following code:

promise.then(function(result) {
  console.log('Got data!', result);
}).catch(function(error) {
  console.log('Error occurred!', error);
});
This is equivalent to:

promise.then(function(result) {
  console.log('Got data!', result);
}).then(undefined, function(error) {
  console.log('Error occurred!', error);
});
Note that if the Promise was rejected and then() does not have a failure callback the control will move forward to the next then() with failure callback or the next catch(). Apart from explicit Promise rejection, catch() is also called when any exception is thrown from the Promise() constructor callback. So, you can also use catch() for logging purposes. Note that we could use try...catch to handle errors, but that is not necessary with Promises as any asynchronous or synchronous error is always caught by catch().
 


No comments:

Post a Comment