JS - 17 - 手写Promise
发布日期:2021-06-30 17:03:22
浏览次数:2
分类:技术文章
本文共 3812 字,大约阅读时间需要 12 分钟。
视频:
手写PROMISE核心代码,提升JAVASCRIPT编程能力,加深JS异步任务理解,ES6必学知识
代码写在 HD.js 里
HD.js
class HD { static PENDING = 'pending'; // 准备 static FUlFILLED = 'fulfilled'; // 解决 static REJECTED = 'rejected'; // 拒绝 constructor(executor) { this.status = HD.PENDING; this.value = null; this.callbacks = []; try { executor(this.resolve.bind(this), this.reject.bind(this)); } catch (error) { this.reject(error); } } resolve(value) { // 异步特性 if (this.status == HD.PENDING) { this.status = HD.FUlFILLED; this.value = value; // 让与resolve同块的方法先执行 setTimeout(() => { // 处理准备时期 then 放入的回调 this.callbacks.forEach(callback => { callback['onFulfilled'](value); }) }) } } reject(reason) { // 异步特性 if (this.status == HD.PENDING) { this.status = HD.REJECTED; this.value = reason; // 让与resolve同块的方法先执行 setTimeout(() => { // 处理准备时期 then 放入的回调 this.callbacks.forEach(callback => { callback['onRejected'](reason); }) }) } } then(onFulfilled, onRejected) { // 没传resolve方法 if (typeof onFulfilled != 'function') { // then值穿透 onFulfilled = () => this.value; } // 没传reject方法 if (typeof onRejected != 'function') { // 没有catch异常 onRejected = function () { throw new Error("Unexpected end of input") } } let promise = new HD((resolve, reject) => { // 准备 if (this.status == HD.PENDING) { this.callbacks.push({ onFulfilled: value => { // 冗余代码的优化 HD.parse(promise, onFulfilled(value), resolve, reject) }, onRejected: reason => { // 冗余代码的优化 HD.parse(promise, onRejected(reason), resolve, reject) } }); } // 处理 else if (this.status == HD.FUlFILLED) { // 等待this.next赋值 setTimeout(() => { // 冗余代码的优化 HD.parse(promise, onFulfilled(this.value), resolve, reject) }) } // 拒绝 else if (this.status == HD.REJECTED) { // 等待this.next赋值 setTimeout(() => { // 冗余代码的优化 HD.parse(promise, onRejected(this.value), resolve, reject) }) } }); return promise; } static parse(promise, result, resolve, reject) { // 禁止同一个then中循环返回 promise if (promise == result) { throw new TypeError('Chaining cycle detected') } try { // 设置结果值 if (result instanceof HD) { result.then(resolve, reject); } else { resolve(result); } } catch (error) { reject(error); } } static resolve(value) { return new HD((resolve, reject) => { HD.parse(null, value, resolve, reject); }) } static reject(value) { return new HD((resolve, reject) => { reject(value) }) } /** * 全成功,返回全部返回值 * 有失败,返回第一个失败信息 * @param {HD} promises */ static all(promises) { return new HD((resolve, reject) => { const values = []; promises.forEach((promise) => { promise.then( value => { values.push(value) if (values.length == promises.length) { resolve(values); } }, reason => { reject(reason) } ) }) }) } /** * race(竞赛) * 返回第一个返回值 * @param {HD} promises */ static race(promises) { return new HD((resolve, reject) => { promises.forEach(promise => { promise.then( value => { return resolve(value); }, reason => { return reject(reason); } ) }) }) }}
race测试代码(其他自行测试吧。。)
Document
转载地址:https://lawsssscat.blog.csdn.net/article/details/104528046 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月17日 07时40分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
静态包中info.plist打进去之后,上线打包ipa报错
2019-04-30
dispatch_after使用方法详解
2019-04-30
iOS Xcode 8 快捷键 (注释 失效 处理)
2019-04-30
self与self class有什么用法上的区别
2019-04-30
清理app应用程序的缓存
2019-04-30
字符串排序和characterAtIndex:i 方法
2019-04-30
自定义pch文件,设置宏定义
2019-04-30
java三大框架的理解
2019-04-30
Apache与Nginx的优缺点比较
2019-04-30
apache和tomcat
2019-04-30
Uinux/linux vi保存退出命令 (如何退出vi)
2019-04-30
Mac下安装mysql服务及基于workbench的使用方法
2019-04-30
mysql 入门基本命令
2019-04-30
编辑器、编译器和链接器的概念和区别
2019-04-30
FTP协议与HTTP协议的区别
2019-04-30
为什么要用堡垒机,堡垒机能给公司带来什么?
2019-04-30
常用的Mysql数据库操作语句大全
2019-04-30
ThinkPHP中 C(),D(),S()
2019-04-30
在Mac环境下配置tomcat
2019-04-30
Web工程目录和tomcat目录
2019-04-30