Promise is a mechanism for JavaScript to avoid the “callback hell” problem in asynchronous operations.
What is a promise?
From here.
The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise is in one of three different states:
Pending: The initial state of a promise.
Fulfilled(resloved): The state of a promise representing a successful operation.
Rejected: The state of a promise representing a failed operation.
Once a promise is fulfilled or rejected, it is immutable (i.e. it can never change again).
Here is an example how to use it: (From here)
1 | // From Jake Archibald's Promises and Back: |
Note the following facts:
When creating a “Promise” instance, pass a function as the constructor parameter which has two parameters, each of them is a function: The former(resolve/fulfill) is the one to be called when the async operation succeeds, which has one parameter that is the returned object of the async operation. The latter(reject) is the one to be called when the async operation fails, which has one parameter that is the error object.
The “Promise” instance has the “then()” method, which will be called when the async operation finishes. The “then()” method accepts either two arguments “resolve/fulfill” and “reject”, or one argument “resolve/fulfill”. The “resolve/fulfill” parameter is a function has one parameter which is the returned object of the async operation. The “reject” parameter is a function has one parameter which is the error object.
The “Promise” instance has the “catch()” method, which will be called when the async operation fails. The “catch()” method accepts one argument “reject”, which is a function has one parameter that is the error object.
What is “Promise.all”?
From here.
If you trigger multiple async interactions but only want to respond when all of them are completed, that’s where “Promise.all” comes in. The “Promise.all” method takes an array of promises and fires one callback once they are all resolved:
1 | Promise.all([promise1, promise2]).then(function(results) { |
An perfect way of thinking about Promise.all is firing off multiple AJAX (via fetch) requests at one time:
1 | var request1 = fetch('/users.json'); |
What is “Promise.race”?
From here.
“Promise.race” is an interesting function: instead of waiting for all promises to be resolved or rejected, “Promise.race” triggers as soon as any promise in the array is resolved or rejected:
1 | var req1 = new Promise(function(resolve, reject) { |
A use case could be triggering a request to a primary source and a secondary source (in case the primary or secondary are unavailable).
Polyfill
In web development, a polyfill is code that implements a feature on web browsers that do not support the feature. Most often, it refers to a JavaScript library that implements an HTML5 web standard, either an established standard (supported by some browsers) on older browsers, or a proposed standard (not supported by any browsers) on existing browsers. Formally, “a polyfill is a shim for a browser API”.
Polyfills allow web developers to use an API regardless of whether it is supported by a browser or not, and usually with minimal overhead. Typically they first check if a browser supports an API, and use it if available, otherwise using their own implementation. Polyfills themselves use other, more supported features, and thus different polyfills may be needed for different browsers. The term is also used as a verb: polyfilling is providing a polyfill for a feature.
For an example, “es6-promise“ is a polyfill of Promise for ES6.
“async/await” in ES7 make it even simpler
ES7 introduce async and await syntax. It makes the asynchronous syntax look prettier and easier to understand, without the “then” and “catch”.
1 | async function(request, response) { |
The simplest explanation of how this works is that await takes a promise, waits for it’s value to be available, and then returns that value.
Note:
“await” takes a promise, waits for it’s value to be available, and then returns that value.
only functions with the “async” keyword can have “await” statement inside. And you can’t have a top level “await” as well. From here.
And you can use “Promise.all” as well:
1 | var P = require("popsicle"); |