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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Vue - 2 - 【归档】、基本概念、【生命周期】、模板语法
下一篇:js - bind、call、apply

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月17日 07时40分06秒