代码拉取完成,页面将自动刷新
简体中文 | English
✨ ES2021 Promise 的实现,基于 ES3 语法拥有超高兼容性,并遵从 ECMA-262 与 Promises/A+ 标准
学习
Promise
的最佳方式是实现它。
能力 | 版本 | 支持 |
---|---|---|
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 | ✔ |
npm i -S promise-for-es
// ES Module
import 'promise-for-es/polyfill';
// CommonJS
require('promise-for-es/polyfill');
// ES Module
import Promise from 'promise-for-es';
// CommonJS
const Promise = require('promise-for-es');
以下面的代码为例:
const executor = (resolutionFunc, rejectionFunc) => {
// 业务逻辑
};
const p1 = new Promise(executor);
p1.then(onFulfilled, onRejected);
p2
;p1
的状态:
onFulfilled
添加到 p1
的 fulfill list,onRejected
添加到 reject list ;onFulfilled
、p2
、p1
的结果 新建一个微任务;onRejected
、p2
、p1
的结果 新建一个微任务;p2
;resolutionFunc
、rejectionFunc
;resolutionFunc
、rejectionFunc
作为参数调用 executor
;value
是 thenable,以 value
新建一个微任务,并返回;p1
的状态为 "fulfilled";p1
的状态为 "rejected";npm run test:aplus
运行 Promises/A+ 合规性测试;npm run test:es6
运行 promises-es6-tests;npm run test:core-js
运行 core-js 关于 Promise 的相关测试;此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。