JavaScript手写Promise
2022/1/28 17:04:14
本文主要是介绍JavaScript手写Promise,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
const PROMISE_STATUS_PENDING = 'pending' const PROMISE_STATUS_FULFILLED = 'fulfilled' const PROMISE_STATUS_REJECTED = 'rejected' // 工具函数 function execFunctionWithCatchError(execFn, value, resolve, reject) { try { const result = execFn(value) resolve(result) } catch(err) { reject(err) } } class LPromise { constructor(executor) { this.status = PROMISE_STATUS_PENDING this.value = undefined this.reason = undefined this.onFulfilledFns = [] this.onRejectedFns = [] const resolve = (value) => { if (this.status === PROMISE_STATUS_PENDING) { // 添加微任务 queueMicrotask(() => { if (this.status !== PROMISE_STATUS_PENDING) return this.status = PROMISE_STATUS_FULFILLED this.value = value this.onFulfilledFns.forEach(fn => { fn(this.value) }) }); } } const reject = (reason) => { if (this.status === PROMISE_STATUS_PENDING) { // 添加微任务 queueMicrotask(() => { if (this.status !== PROMISE_STATUS_PENDING) return this.status = PROMISE_STATUS_REJECTED this.reason = reason this.onRejectedFns.forEach(fn => { fn(this.reason) }) }) } } try { executor(resolve, reject) } catch (err) { reject(err) } } then(onFulfilled, onRejected) { const defaultOnRejected = err => { throw err } onRejected = onRejected || defaultOnRejected const defaultOnFulfilled = value => { return value } onFulfilled = onFulfilled || defaultOnFulfilled return new LPromise((resolve, reject) => { // 1.如果在then调用的时候, 状态已经确定下来 if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) { execFunctionWithCatchError(onFulfilled, this.value, resolve, reject) } if (this.status === PROMISE_STATUS_REJECTED && onRejected) { execFunctionWithCatchError(onRejected, this.reason, resolve, reject) } // 2.将成功回调和失败的回调放到数组中 if (this.status === PROMISE_STATUS_PENDING) { if (onFulfilled) this.onFulfilledFns.push(() => { execFunctionWithCatchError(onFulfilled, this.value, resolve, reject) }) if (onRejected) this.onRejectedFns.push(() => { execFunctionWithCatchError(onRejected, this.reason, resolve, reject) }) } }) } catch(onRejected) { return this.then(undefined, onRejected) } finally(onFinally) { this.then(() => { onFinally() }, () => { onFinally() }) } static resolve(value) { return new LPromise((resolve) => resolve(value)) } static reject(reason) { return new LPromise((resolve, reject) => reject(reason)) } static all(promises) { // 问题关键: 什么时候要执行resolve, 什么时候要执行reject return new LPromise((resolve, reject) => { const values = [] promises.forEach(promise => { promise.then(res => { values.push(res) if (values.length === promises.length) { resolve(values) } }, err => { reject(err) }) }) }) } static allSettled(promises) { return new LPromise((resolve) => { const results = [] promises.forEach(promise => { promise.then(res => { results.push({ status: PROMISE_STATUS_FULFILLED, value: res}) if (results.length === promises.length) { resolve(results) } }, err => { results.push({ status: PROMISE_STATUS_REJECTED, reason: err}) if (results.length === promises.length) { resolve(results) } }) }) }) } static race(promises) { return new LPromise((resolve, reject) => { promises.forEach(promise => { // promise.then(res => { // resolve(res) // }, err => { // reject(err) // }) promise.then(resolve, reject) }) }) } static any(promises) { // resolve必须等到有一个成功的结果 // reject所有的都失败才执行reject const reasons = [] return new LPromise((resolve, reject) => { promises.forEach(promise => { promise.then(resolve, err => { reasons.push(err) if (reasons.length === promises.length) { reject(new AggregateError(reasons)) } }) }) }) } }
这篇关于JavaScript手写Promise的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-09必试!帮 J 人团队解决物流错发漏发的软件神器!
- 2025-01-09不容小觑!助力 J 人物流客服安抚情绪的软件!
- 2025-01-09为什么医疗团队协作离不开智能文档工具?
- 2025-01-09惊叹:J 人团队用啥软件让物流服务快又准?
- 2025-01-09如何利用数据分析工具优化项目资源分配?4种工具推荐
- 2025-01-09多学科协作难?这款文档工具可以帮你省心省力
- 2025-01-09团队中的技术项目经理TPM:工作内容与资源优化策略
- 2025-01-09JIT生产管理法:优化流程,提升竞争力的秘诀
- 2025-01-092024全球互联网流量分析报告
- 2025-01-09如何提升学校行政管理中的进度追踪效率?4个实用策略和3款工具推荐