Convert Figma logo to code with AI

stefanpenner logoes6-promise

A polyfill for ES6-style Promises

7,290
594
7,290
26

Top Related Projects

20,444

:bird: :zap: Bluebird is a full featured promise library with unmatched performance.

2,577

Bare bones Promises/A+ implementation

14,940

A promise library for JavaScript

3,613

A lightweight library that provides tools for organizing asynchronous code

Lightweight ES6 Promise polyfill for the browser and node. A+ Compliant

A polyfill for ES6-style Promises

Quick Overview

es6-promise is a polyfill for ES6-style Promises, providing a lightweight implementation of the Promise API for environments that don't natively support it. It aims to be fully compliant with the ES6 specification while offering a small footprint and high performance.

Pros

  • Lightweight and fast implementation of Promises
  • Fully compliant with the ES6 specification
  • Works in both browser and Node.js environments
  • Extensive test suite ensuring reliability

Cons

  • May be unnecessary for modern environments that already support native Promises
  • Doesn't include additional features beyond the standard Promise API
  • Requires polyfilling in older browsers, potentially increasing bundle size

Code Examples

  1. Creating and using a Promise:
const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Success!');
  }, 1000);
});

promise.then(result => console.log(result));
  1. Chaining Promises:
function fetchUser(id) {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ id, name: 'John Doe' }), 1000);
  });
}

function fetchUserPosts(user) {
  return new Promise((resolve) => {
    setTimeout(() => resolve([{ id: 1, title: 'Hello World' }]), 1000);
  });
}

fetchUser(1)
  .then(user => fetchUserPosts(user))
  .then(posts => console.log(posts));
  1. Using Promise.all():
const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve) => setTimeout(() => resolve('foo'), 100));

Promise.all([promise1, promise2])
  .then(values => console.log(values)); // [3, 'foo']

Getting Started

To use es6-promise in your project, first install it via npm:

npm install es6-promise

Then, you can import and use it in your JavaScript code:

import { Promise } from 'es6-promise';

// If you want to polyfill the global scope:
import 'es6-promise/auto';

// Now you can use Promises as usual
const myPromise = new Promise((resolve, reject) => {
  // Your asynchronous code here
});

myPromise.then(result => {
  console.log(result);
}).catch(error => {
  console.error(error);
});

Competitor Comparisons

20,444

:bird: :zap: Bluebird is a full featured promise library with unmatched performance.

Pros of Bluebird

  • Significantly faster performance in most scenarios
  • More extensive feature set, including advanced error handling and utilities
  • Better browser support, especially for older versions

Cons of Bluebird

  • Larger file size, which may impact load times in browser environments
  • More complex API, potentially steeper learning curve for beginners
  • Not a native implementation of the ES6 Promise specification

Code Comparison

es6-promise:

new Promise((resolve, reject) => {
  setTimeout(() => resolve('Done'), 1000);
}).then(result => console.log(result));

Bluebird:

new Promise((resolve, reject) => {
  setTimeout(() => resolve('Done'), 1000);
}).then(result => console.log(result))
  .catch(error => console.error(error))
  .finally(() => console.log('Cleanup'));

Both libraries provide similar basic Promise functionality, but Bluebird offers additional methods like finally() for more advanced use cases. Bluebird also includes utilities for working with collections and controlling concurrency, which are not present in es6-promise.

While es6-promise aims to be a polyfill for native Promises, Bluebird goes beyond the standard specification to offer a more feature-rich alternative. The choice between the two depends on project requirements, performance needs, and the desired balance between simplicity and advanced functionality.

2,577

Bare bones Promises/A+ implementation

Pros of promise

  • More comprehensive API with additional utility methods
  • Better support for advanced use cases and error handling
  • Active development and maintenance

Cons of promise

  • Larger bundle size due to additional features
  • Steeper learning curve for beginners
  • May require polyfills for older browsers

Code Comparison

es6-promise:

var promise = new Promise(function(resolve, reject) {
  // ...
});

promise.then(function(value) {
  // ...
});

promise:

var promise = new Promise(function(resolve, reject) {
  // ...
});

promise
  .then(function(value) {
    // ...
  })
  .catch(function(error) {
    // ...
  })
  .finally(function() {
    // ...
  });

The code comparison shows that promise offers more chainable methods like catch and finally, providing better control over promise resolution and error handling. es6-promise focuses on core functionality, while promise extends the API with additional features.

Both libraries implement the Promise/A+ specification, ensuring compatibility with native Promises. However, promise offers a richer set of utilities and methods for more complex asynchronous operations, making it suitable for advanced use cases. es6-promise, on the other hand, provides a lightweight implementation that closely mirrors the ECMAScript 6 Promise specification.

14,940

A promise library for JavaScript

Pros of Q

  • More comprehensive feature set, including advanced functionality like progress tracking and cancellation
  • Longer history and wider adoption in the JavaScript community
  • Extensive documentation and examples available

Cons of Q

  • Larger file size and potentially higher performance overhead
  • Less focused on ES6 Promise compatibility
  • Steeper learning curve due to additional features and complexity

Code Comparison

Q:

Q.fcall(function () {
    return "Hello";
})
.then(function (result) {
    console.log(result);
});

es6-promise:

new Promise(function(resolve) {
    resolve("Hello");
})
.then(function(result) {
    console.log(result);
});

Key Differences

  • Q offers a more extensive API with additional methods and features
  • es6-promise focuses on providing a lightweight, standards-compliant Promise implementation
  • Q includes support for Node.js-style callbacks, while es6-promise strictly adheres to the Promise/A+ specification

Use Cases

  • Choose Q for complex asynchronous workflows or when additional features are required
  • Opt for es6-promise when aiming for a minimal, standards-compliant Promise implementation or working in environments where file size is a concern

Community and Maintenance

  • Q has a larger community and longer history, but updates are less frequent
  • es6-promise is actively maintained and focuses on keeping up with the latest ECMAScript specifications
3,613

A lightweight library that provides tools for organizing asynchronous code

Pros of RSVP.js

  • More comprehensive API with additional features like all(), race(), hash(), and filter()
  • Better suited for complex asynchronous operations and workflows
  • Actively maintained with regular updates and improvements

Cons of RSVP.js

  • Larger file size and potentially higher overhead compared to es6-promise
  • May have a steeper learning curve due to additional features and complexity
  • Not as focused on strict ES6 Promise compatibility

Code Comparison

RSVP.js:

RSVP.all([promise1, promise2]).then(function(results) {
  console.log(results);
}).catch(function(error) {
  console.error(error);
});

es6-promise:

Promise.all([promise1, promise2]).then(function(results) {
  console.log(results);
}).catch(function(error) {
  console.error(error);
});

The code comparison shows that both libraries support similar Promise functionality, but RSVP.js offers additional methods and features beyond the basic Promise API. While es6-promise focuses on providing a polyfill for ES6 Promises, RSVP.js extends the functionality with more advanced asynchronous patterns and utilities.

Both libraries are valuable depending on the project requirements. es6-promise is ideal for projects needing a lightweight ES6 Promise implementation, while RSVP.js is better suited for more complex asynchronous workflows and applications requiring additional Promise-based utilities.

Lightweight ES6 Promise polyfill for the browser and node. A+ Compliant

Pros of promise-polyfill

  • Smaller bundle size (approximately 1.5kb gzipped)
  • Simpler implementation, easier to understand and maintain
  • Includes additional features like Promise.allSettled() and Promise.any()

Cons of promise-polyfill

  • Less comprehensive test suite compared to es6-promise
  • May not cover all edge cases handled by es6-promise
  • Fewer contributors and less frequent updates

Code Comparison

es6-promise:

var Promise = require('es6-promise').Promise;

var promise = new Promise(function(resolve, reject) {
  // ...
});

promise-polyfill:

require('promise-polyfill').polyfill();

var promise = new Promise(function(resolve, reject) {
  // ...
});

Both libraries provide similar usage, but promise-polyfill automatically polyfills the global Promise object, while es6-promise requires explicit usage or manual polyfilling.

Summary

promise-polyfill offers a lightweight alternative to es6-promise with a smaller footprint and some additional features. However, es6-promise has a more extensive test suite and a larger community. The choice between the two depends on specific project requirements, such as bundle size constraints or the need for additional Promise methods.

A polyfill for ES6-style Promises

Pros of es6-promise

  • Well-established and widely used implementation of ES6 Promises
  • Comprehensive test suite ensuring reliability
  • Actively maintained with regular updates and bug fixes

Cons of es6-promise

  • Larger bundle size compared to native Promise implementations
  • May have slightly slower performance in some scenarios
  • Requires polyfilling for older browsers that don't support ES6 Promises

Code Comparison

Both repositories contain the same codebase, as they are the same project. Here's a sample of the core Promise implementation:

function Promise(resolver) {
  this[PROMISE_ID] = nextId();
  this._result = this._state = undefined;
  this._subscribers = [];

  if (noop !== resolver) {
    typeof resolver !== 'function' && needsResolver();
    this instanceof Promise ? initializePromise(this, resolver) : needsNew();
  }
}

This code snippet showcases the Promise constructor, which is identical in both repositories. The implementation follows the ES6 Promise specification, providing a consistent and reliable Promise implementation across different environments.

As both repositories are the same project, there are no significant differences in the codebase or functionality. The choice between them would depend on personal preference or specific project requirements.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ES6-Promise (subset of rsvp.js) Build Status

This is a polyfill of the ES6 Promise. The implementation is a subset of rsvp.js extracted by @jakearchibald, if you're wanting extra features and more debugging options, check out the full library.

For API details and how to use promises, see the JavaScript Promises HTML5Rocks article.

Downloads

CDN

To use via a CDN include this in your html:

<!-- Automatically provides/replaces `Promise` if missing or broken. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script> 

<!-- Minified version of `es6-promise-auto` below. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script> 

Node.js

To install:

yarn add es6-promise

or

npm install es6-promise

To use:

var Promise = require('es6-promise').Promise;

Usage in IE<9

catch and finally are reserved keywords in IE<9, meaning promise.catch(func) or promise.finally(func) throw a syntax error. To work around this, you can use a string to access the property as shown in the following example.

However most minifiers will automatically fix this for you, making the resulting code safe for old browsers and production:

promise['catch'](function(err) {
  // ...
});
promise['finally'](function() {
  // ...
});

Auto-polyfill

To polyfill the global environment (either in Node or in the browser via CommonJS) use the following code snippet:

require('es6-promise').polyfill();

Alternatively

require('es6-promise/auto');

Notice that we don't assign the result of polyfill() to any variable. The polyfill() method will patch the global environment (in this case to the Promise name) when called.

Building & Testing

You will need to have PhantomJS installed globally in order to run the tests.

npm install -g phantomjs

  • npm run build to build
  • npm test to run tests
  • npm start to run a build watcher, and webserver to test
  • npm run test:server for a testem test runner and watching builder

NPM DownloadsLast 30 Days