promise-for-es

1.0.0 • Public • Published
Promises/A+ logo

简体中文 | English

Promise For ES

A ES2021 Promise implementation based on ES3 has high compatibility, and comply with ECMA-262 and Promises/A+

The best way to learn Promise is to implement it.

Feature

  1. Base on ES3, almost all browsers are supported;
  2. Comply with ECMA-262 and Promises/A+, pass the Promises/A+ compliance test, and other related tests;
  3. Implement the new features about Promise of ES2018、ES2020、ES2021;

Support

Ability Version Support
new Promise(executor) ES2015
Promise.prototype.then(onFulfilled, onRejected) ES2015
Promise.prototype.catch(onRejected) ES2015
Promise.prototype.finally(onFinally) ES2018
Promise.resolve(value) ES2015
Promise.reject(reason) ES2015
Promise.all(iterable) ES2015
Promise.race(iterable) ES2015
Promise.allSettled(iterable) ES2020
Promise.any(iterable) ES2021

Install

npm i -S promise-for-es

Usage

  1. As a polyfill
// ES Module
import 'promise-for-es/polyfill';
// CommonJS
require('promise-for-es/polyfill');
  1. As a ponyfill
// ES Module
import Promise from 'promise-for-es';
// CommonJS
const Promise = require('promise-for-es');

Core logic

Using the example below:

const executor = (resolutionFunc, rejectionFunc) => {
    // business logic
};
const p1 = new Promise(executor);
p1.then(onFulfilled, onRejected);

p1.then(onFulfilled, onRejected)

  1. Create a new Promise object p2 ;
  2. Check the state of p1 :
    1. If "pending", push onFulfilled into the fulfill list of p1, and push onRejected into the reject list;
    2. If "fulfilled", create a micro task with onFulfilled, p2 and the result of p1 ;
    3. If "rejected", create a micro task with onRejected, p2 and the result of p1 ;
  3. return p2 ;

new Promise(executor)

  1. Create the resolving functions: resolutionFunc, rejectionFunc ;
  2. Call executor with resolutionFunc and rejectionFunc as the arguments;

resolutionFunc(value)

  1. If any resolving function has been called, return;
  2. If value is thenable, create a micro task with value, return;
  3. Change the state of p1 to "fulfilled";
  4. Create a micro task for each element of fulfill list;

rejectionFunc(reason)

  1. If any resolving function has been called, return;
  2. Change the state of p1 to "rejected";
  3. Create a micro task for each element of reject list;

Test

  1. npm run test:aplus to run Promises/A+ compliance test;
  2. npm run test:es6 to run promises-es6-tests;
  3. npm run test:core-js to run the core-js tests about Promise;

Reference

  1. ECMA-262
  2. Promises/A+

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    37
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    37
  • 0.0.0
    0

Package Sidebar

Install

npm i promise-for-es

Weekly Downloads

37

Version

1.0.0

License

Apache-2.0

Unpacked Size

124 kB

Total Files

19

Last publish

Collaborators

  • ambit_tsai