An Overview of JavaScript Promises

Well, this has come like a Christmas gift to all JavaScript developers. You will be glad to know that Promises are now a part of standard JavaScript. Chrome 32 beta has already implemented the basic Promise API. The concept of Promises is not new to web development. Many of us have already used Promises in the form of several JS libraries such as Q, when, RSVP.js, etc. Even jQuery has something called Deferred which is similar to a Promise. But having native support for Promises in JavaScript is really amazing. This tutorial will cover the basics of Promises and show how you can leverage them in your JS development.

Note: This is still an experimental feature. Only Chrome 32 beta and the latest Firefox nightly currently support it.

Overview

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 caveat being that the actual data is not available yet. It will become available when the request completes and a response comes back from the web service. In the meantime 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.

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.

In the previous code, the Promise() constructor callback contains the asynchronous code used to get data from remote service. Here, we just create an Ajax request to http://api.icndb.com/jokes/random which returns a random joke. When a JSON response is received from the remote server, it is passed to resolve(). In case of any error, reject() is called with an Error object.

When we instantiate a Promise object we get a proxy to the data that will be available in future. In our case we are expecting some data to be returned from the remote service at some point in future. So, how do we know when the data becomes available? This is where the Promise.then() function is used. The function then() takes two arguments: a success callback and a failure callback. These callbacks are called when the Promise is settled (i.e. either fulfilled or rejected). If the Promise was fulfilled, the success callback will be fired with the actual data you passed to resolve(). If the Promise was rejected, the failure callback will be called. Whatever you passed to reject() will be passed as an argument to this callback.

Try this Plunkr example. Simply refresh the page to view a new random joke. Also, open up your browser console so that you can see the order in which the different parts of the code are executed. Also, note that a Promise can have three states:

pending (not fulfilled or rejected)

fulfilled

rejected

The Promise.status property, which is code-inaccessible and private, gives information about these states. Once a Promise is rejected or fulfilled, this status gets permanently associated with it. This means a Promise can succeed or fail only once. If the Promise has already been fulfilled and later you attach a then() to it with two callbacks the success callback will be correctly called. So, in the world of Promises, we are not interested in knowing when the Promise is settled. We are only concerned with the final outcome of the Promise.

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:

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().

Conclusion

This was just a brief introduction to JavaScript’s new Promises API. Clearly it lets us write asynchronous code very easily. We can proceed as usual without knowing what value is going to be returned from the asynchronous code in the future. There is more to the API, which has not been covered here. To learn more about Promises, browse the following resources, and stay tuned to SitePoint!

Here both, the asynchronous error from httpget and the synchronous SyntaxError from JSON.parse will end up in the same .catch() and there is no need for separate error handling tactics. You are missing the another 50% point of promises if you use try-catch

http://gadgeticworld.com/ Sandeep Panda

Agree with you. I rephrased the line about try/catch.

Craig N.

You are detecting if Promises is supported in the first line of code before instantiating the Promise object. However, if the browser doesn’t support Promises, a typical person’s code is unlikely to fork into a code block that doesn’t rely on Promises, otherwise there’s no point in using Promises in the first place if you have code that can work without Promises!

http://www.earwicker.com Daniel Earwicker

More likely that you would pull in a library that adds window.Promises if none already exists.

Jingqi Xie

What would “this” mean in the callback of a promise?

http://gadgeticworld.com/ Sandeep Panda

Inside the Promise constructor callback ‘this’ refers to Window object. If you have downloaded the Plunkr example, try console.log(this) inside the callback and check what it prints!

Jingqi Xie

Thanks :)

andrewstrader

Fortunately, there’s no need to wait for “window.Promise” to become widely available or polyfilled, because it’s just a pattern for an abstract interface. As long as you provide a chainable “then” method that takes two callbacks, it doesn’t matter how you construct the promise or what internal mechanics you use to resolve or reject it.