What is promise-polyfill?
The promise-polyfill npm package is a lightweight implementation of the ES6 Promise specification. It is designed to provide a simple and efficient way to use Promises in environments that do not natively support them, such as older browsers.
What are promise-polyfill's main functionalities?
Basic Promise Usage
This code demonstrates the basic usage of a Promise. It creates a new Promise that resolves with the message 'Success!' after 1 second, and then logs the message to the console.
const Promise = require('promise-polyfill');
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Success!');
}, 1000);
});
promise.then((message) => {
console.log(message); // 'Success!'
});
Chaining Promises
This code demonstrates how to chain multiple .then() calls to handle a sequence of asynchronous operations. Each .then() call receives the resolved value from the previous Promise and returns a new value.
const Promise = require('promise-polyfill');
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1);
}, 1000);
});
promise
.then((value) => {
console.log(value); // 1
return value + 1;
})
.then((value) => {
console.log(value); // 2
return value + 1;
})
.then((value) => {
console.log(value); // 3
});
Handling Errors
This code demonstrates how to handle errors in Promises using the .catch() method. If the Promise is rejected, the error message is logged to the console.
const Promise = require('promise-polyfill');
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
reject('Error occurred!');
}, 1000);
});
promise
.then((value) => {
console.log(value);
})
.catch((error) => {
console.error(error); // 'Error occurred!'
});
Other packages similar to promise-polyfill
es6-promise
The es6-promise package is another polyfill for the ES6 Promise specification. It provides a similar API to promise-polyfill and is widely used in environments that lack native Promise support. Compared to promise-polyfill, es6-promise is slightly larger in size but offers more comprehensive test coverage and compatibility.
bluebird
Bluebird is a fully-featured Promise library that offers a wide range of additional features and optimizations beyond the ES6 Promise specification. It includes utilities for working with collections, cancellation, and more. While it is more powerful and feature-rich than promise-polyfill, it is also significantly larger in size.
q
Q is a popular Promise library that predates the ES6 Promise specification. It provides a rich set of features for working with asynchronous code, including support for deferred objects and advanced error handling. Q is more feature-rich than promise-polyfill but has a larger footprint and a different API.
Promise [![Build Status](https://travis-ci.org/taylorhakes/promise-polyfill.png?branch=master)](https://travis-ci.org/taylorhakes/promise-polyfill) ![https://david-dm.org/taylorhakes/promise-polyfill/dev-status.png](https://david-dm.org/taylorhakes/promise-polyfill/dev-status.png)
=============
Lightweight promise polyfill for the browser and node. A+ Compliant. It is a perfect polyfill IE, Firefox or any other browser that does not support native promises.
This implementation is based on then/promise. It has been changed to use the prototype for performance and memory reasons.
For API information about Promises, please check out this article HTML5Rocks article.
It is extremely lightweight. < 1kb Gzipped
Downloads
Node
npm install promise-polyfill
Simple use
var prom = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if (/* everything turned out fine */) {
resolve("Stuff worked!");
}
else {
reject(new Error("It broke"));
}
});
// Do something when async done
prom.then(function() {
...
});
Performance
By default promise-polyfill uses setImmediate
, but falls back to setTimeout
for executing asynchronously. If a browser does not support setImmediate
, you may see performance issues.
Use a setImmediate
polyfill to fix this issue. setAsap or setImmediate work well.
Testing
npm install
npm test
License
MIT