1 Star 8 Fork 0

zi.Yang / toy-promise

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Toy Promise

aplus 注释截图

  • 如下图,以ES262- 编号开头的,就是对应的 Promise/ES262 规范要求

es262 注释截图

开发环境

  • Node.js v12+
  • VScode
  • typescript v4.9.3

项目安装

npm install

开发编译

npm run dev

编译打包

npm run build

代码风格检测

npm run lint

Promise/A+ 规范测试

npm run test:aplus

ES262-Promise 规范测试

npm run test:es6

注:当前并未提供单测脚本,运行版本单测文件需手动执行 node test/version/v1.test

目录结构

.
├── LICENSE ------------------------------------- 开源许可
├── README.md ----------------------------------- 说明文档
├── dist  --------------------------------------- ts 编译输出目录
├── readme  ------------------------------------- readme.md 相关资料
├── index  -------------------------------------- index 入口
├── lib ----------------------------------------- toy-promise 源码
├── mock ---------------------------------------- mock 异步方法
├── package.json -------------------------------- package.json
├── test ---------------------------------------- 测试代码目录
└── tsconfig.json ------------------------------- tsconfig.json

如何调试

  • toy-promise 的核心实现源码在 /lib
/lib
├── common.ts ---------------------------------- 通用方法实现
├── createPromiseInstance.ts ------------------- 创建 Promise 实例的工厂函数
├── resolvePromise.ts -------------------------- then 递归穿透方法
├── throwError.ts ------------------------------ 抛出异常函数
└── index.ts  ---------------------------------- Promise 核心实现
  • 本仓库没有使用单测工具,所以需要手动编写测试用例加以测试
/test/version
├── common.js --------------------------------- 封装的一些测试常用方法
├── v1.test.js -------------------------------- v1 版本测试用例
├── v2.test.js  ------------------------------- v2 版本测试用例
└── v3.test.js  ------------------------------- v3 版本测试用例
  • 通过 node 运行对应的单测文件即可
node /test/version/v1.test

功能清单

功能 TP 版本 ES 版本 状态
new Promise(executor) v1.0 ES2015 :white_check_mark:
Promise.prototype.then(onFulfilled, onRejected) v1.0 ES2015 :white_check_mark:
Promise.prototype.catch(onRejected) v2.0 ES2015 :white_check_mark:
Promise.resolve(value) v2.0 ES2015 :white_check_mark:
Promise.reject(reason) v2.0 ES2015 :white_check_mark:
Promise.all(iterable) v3.0 ES2015 :white_check_mark:
Promise.prototype.finally(onFinally) v3.0 ES2018 :white_check_mark:
Promise.race(iterable) v4.0 ES2015 :white_check_mark:
Promise.allSettled(iterable) v5.0 ES2020 :white_check_mark:
Promise.any(iterable) v5.0 ES2021 :white_check_mark:

版本说明

由于 Promise/A+ 规范中只要求了 then 方法,所以其余方法根据 ES262 规范实现。

每个版本都提供了相应的 Tag 以便查阅。

v1 版本

  • 该版本参照 Promise/A+ 规范完成实现
  • 通过 Promise/A+ 规范工具方法测试

V2 版本

  • 实现了 Promise.resolve
  • 实现了 Promise.reject
  • 实现了 Promise.prototype.catch

v3 版本

  • 实现了 Promise.all
  • 实现了 Promise.prototype.finally
  • 重写了 Promise.prototype.catch

v4 版本

  • 实现了 Promise.race
  • 完全重写 v2 版本所实现功能
  • 引入 ES262-Promise 规范测试工具
  • 通过 ES262-Promise 所有规范校验

v5 版本

  • 实现了 Promise.any
  • 实现了 Promise.allSettled
  • 引入工厂函数,重构 ToyPromise
  • 优化了 TS 类型,引入了 ESLint 优化了代码风格
  • 引入了 rollup 增加了打包功能,优化了开发时体验

Promise/A+ 测试截图

Promise/A+ 测试工具完整性测试截图

ES262-Promise 测试截图

es262 测试通过截图

MIT License Copyright (c) 2022 zi.Yang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于 Promise/A+ 和 Promise/ES262 规范实现的 Promise。 可用于学习实现 Promise 以及探究 Promise 底层机制及原理 展开 收起
TypeScript 等 2 种语言
MIT
取消

发行版 (4)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/Alessandro-pang/toy-promise.git
git@gitee.com:Alessandro-pang/toy-promise.git
Alessandro-pang
toy-promise
toy-promise
master

搜索帮助