promise-polyfill
Lightweight ES6 Promise polyfill for the browser and node. A+ Compliant
Top Related Projects
A polyfill for ES6-style Promises
:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
A promise library for JavaScript
A lightweight library that provides tools for organizing asynchronous code
Quick Overview
Promise-polyfill is a lightweight ES6 Promise polyfill for browsers and Node.js. It aims to be a complete and standards-compliant implementation, providing Promise functionality for environments that don't natively support it or have incomplete implementations.
Pros
- Lightweight and fast implementation
- Fully compliant with the ES6 Promise specification
- Works in both browser and Node.js environments
- Includes additional features like Promise.allSettled() and Promise.any()
Cons
- May not be necessary for modern environments that already support Promises
- Adds extra code to your bundle if you're not using a build process with proper tree-shaking
- Doesn't include some non-standard Promise extensions found in other libraries
Code Examples
- Basic Promise usage:
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Success!');
}, 1000);
});
myPromise.then(result => console.log(result));
- Chaining Promises:
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
- Using Promise.all():
const promises = [
fetch('https://api.example.com/data1'),
fetch('https://api.example.com/data2'),
fetch('https://api.example.com/data3')
];
Promise.all(promises)
.then(responses => Promise.all(responses.map(r => r.json())))
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Getting Started
To use promise-polyfill in your project, first install it via npm:
npm install promise-polyfill --save
Then, import it at the entry point of your application:
import 'promise-polyfill/src/polyfill';
// Now you can use Promises in your code, even in environments that don't support them natively
For browser usage without a build process, you can include it via a script tag:
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
Competitor Comparisons
A polyfill for ES6-style Promises
Pros of es6-promise
- More comprehensive implementation of the ES6 Promise specification
- Better support for advanced Promise features like Promise.race() and Promise.all()
- Larger community and more frequent updates
Cons of es6-promise
- Larger file size, which may impact performance in some applications
- More complex codebase, potentially making it harder to debug or customize
- Slightly steeper learning curve for developers new to Promises
Code Comparison
es6-promise:
var Promise = require('es6-promise').Promise;
var promise = new Promise(function(resolve, reject) {
// Async operation here
if (/* operation successful */) {
resolve('Success!');
} else {
reject('Failure!');
}
});
promise-polyfill:
var Promise = require('promise-polyfill').default;
var promise = new Promise(function(resolve, reject) {
// Async operation here
if (/* operation successful */) {
resolve('Success!');
} else {
reject('Failure!');
}
});
Both libraries provide similar basic Promise functionality, but es6-promise offers a more complete implementation of the ES6 specification. promise-polyfill is lighter and simpler, which may be preferable for projects with basic Promise needs or size constraints.
:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
Pros of Bluebird
- Offers more advanced features and utilities beyond the standard Promise API
- Generally provides better performance, especially in older browsers
- Has extensive documentation and a large community
Cons of Bluebird
- Larger file size, which may impact load times and bundle sizes
- Steeper learning curve due to additional features and complexity
- Less frequently updated in recent years
Code Comparison
Promise-polyfill:
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('Success!');
}, 1000);
});
Bluebird:
var Promise = require('bluebird');
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('Success!');
}, 1000);
});
The basic usage is similar, but Bluebird offers additional methods and utilities:
Promise.map(items, function(item) {
return processItem(item);
}).then(function(results) {
console.log(results);
});
Promise-polyfill focuses on providing a lightweight polyfill for native Promises, while Bluebird offers a more feature-rich alternative with performance optimizations. Choose Promise-polyfill for simple Promise support in older environments, and Bluebird for advanced features and optimizations in larger applications.
A promise library for JavaScript
Pros of Q
- More comprehensive feature set, including advanced functionality like progress tracking and cancellation
- Well-established and battle-tested in production environments
- Extensive documentation and community support
Cons of Q
- Larger file size and potentially higher performance overhead
- More complex API, which may be overkill for simpler use cases
- Less actively maintained in recent years
Code Comparison
Q:
Q.fcall(function () {
return 10;
})
.then(function (result) {
console.log(result);
});
promise-polyfill:
Promise.resolve(10)
.then(function (result) {
console.log(result);
});
Summary
Q is a more feature-rich and mature library, offering advanced functionality beyond basic promises. However, promise-polyfill provides a lightweight alternative that closely mimics the native Promise API, making it a better choice for simple use cases or when bundle size is a concern. Q's syntax is slightly more verbose, while promise-polyfill adheres closely to the standard Promise syntax. Consider your project's specific needs when choosing between these libraries.
A lightweight library that provides tools for organizing asynchronous code
Pros of RSVP.js
- More comprehensive API with additional features like
RSVP.hash
andRSVP.filter
- Better suited for larger applications with complex asynchronous operations
- Actively maintained with regular updates and improvements
Cons of RSVP.js
- Larger file size, which may impact performance in smaller projects
- Steeper learning curve due to additional features and complexity
- May be overkill for simple projects that only require basic Promise functionality
Code Comparison
RSVP.js:
import RSVP from 'rsvp';
RSVP.resolve('Success').then(function(value) {
console.log(value);
});
promise-polyfill:
import Promise from 'promise-polyfill';
Promise.resolve('Success').then(function(value) {
console.log(value);
});
Both libraries provide similar basic Promise functionality, but RSVP.js offers additional methods and features for more complex scenarios. The choice between the two depends on the project's specific requirements, with promise-polyfill being more suitable for smaller projects or when a lightweight solution is needed, while RSVP.js is better for larger applications with more complex asynchronous operations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Promise Polyfill
Lightweight ES6 Promise polyfill for the browser and node. Adheres closely to the spec. It is a perfect polyfill IE or any other browser that does not support native promises.
For API information about Promises, please check out this article HTML5Rocks article.
It is extremely lightweight. < 1kb Gzipped
Browser Support
IE8+, Chrome, Firefox, IOS 4+, Safari 5+, Opera
NPM Use
npm install promise-polyfill --save-exact
Bower Use
bower install promise-polyfill
CDN Polyfill Use
This will set a global Promise object if the browser doesn't already have window.Promise
.
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
Downloads
Simple use
If you would like to add a global Promise object (Node or Browser) if native Promise doesn't exist (polyfill Promise). Use the method below. This is useful if you are building a website and want to support older browsers. Javascript library authors should NOT use this method.
import 'promise-polyfill/src/polyfill';
If you would like to not affect the global environment (sometimes known as a ponyfill, you can import the base module. This is nice for library authors or people working in environment where you don't want to affect the global environment.
import Promise from 'promise-polyfill';
If using require
with Webpack 2+ (rare), you need to specify the default import
var Promise = require('promise-polyfill').default;
then you can use like normal Promises
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"));
}
});
prom.then(function(result) {
// Do something when async done
});
Performance
By default promise-polyfill uses setImmediate
, but falls back to setTimeout
for executing asynchronously. If a browser does not support setImmediate
(IE/Edge are the only browsers with setImmediate), you may see performance
issues. Use a setImmediate
polyfill to fix this issue.
setAsap or
setImmediate work well.
If you polyfill window.setImmediate
or use Promise._immediateFn = yourImmediateFn
it will be used instead of window.setTimeout
npm install setasap --save
import Promise from 'promise-polyfill/src/polyfill';
import setAsap from 'setasap';
Promise._immediateFn = setAsap;
Unhandled Rejections
promise-polyfill will warn you about possibly unhandled rejections. It will show
a console warning if a Promise is rejected, but no .catch
is used. You can
change this behavior by doing.
-NOTE: This only works on promise-polyfill Promises. Native Promises do not support this function
Promise._unhandledRejectionFn = <your reject error handler>;
If you would like to disable unhandled rejection messages. Use a noop like below.
Promise._unhandledRejectionFn = function(rejectError) {};
Testing
npm install
npm test
License
MIT
Top Related Projects
A polyfill for ES6-style Promises
:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
A promise library for JavaScript
A lightweight library that provides tools for organizing asynchronous code
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot