O'Reilly logo

Mastering JavaScript High Performance by Chad R. Adams

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Promises

JavaScript promises are also a new way to optimize our JavaScript code; the idea of promises is you have a function that is chained to a main function, and is fired in sequential order as it's written. Here is how it's structured. First, we create a new object using the Promise object, and, inside the parentheses, we write the main function, and assign the new promise object to a variable.

One thing to note before continuing is that JavaScript promises are EcmaScript 6- specific. So, for this section, we will need to test our code in an EcmaScript 6-ready browser such as Google Chrome.

Next, with our promise variable, we use the then keyword, which in actuality works just like a function. But it only fires when our root promise function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required