const getType = value => { return Object.prototype.toString.call(value).match(/^\[object (.*)\]$/)[1];}const isFuntion = value => { return getType(value) === 'Function'} // 定义好三种状态const PENDING = 'pending'const FULFILLED = 'fulfilled'const REJECTED = 'rejected'// 类class MyPromise { constructor(fn) { this.status = PENDING this.value = null this.reason = null this.callbacks = [] // 存放回掉函数 try { fn(this.resolve, this.reject) } catch (error) { this.reject(error) } } static resolve(value) { return new MyPromise((res) => res(value)) } static reject(reason) { return new MyPromise((res, rej) => rej(reason)) } resolve(value) { // 已经从pending改变了,不能在改变成其他状态 if (this.status !== PENDING) return this.status = FULFILLED this.value = value this.handleCallback() } reject(reason) { // 已经从pending改变了,不能在改变成其他状态 if (this.status !== PENDING) return this.status = REJECTED this.reason = reason this.handleCallback() } // then方法必须在状态改变之后才能执行 then(onFulfilled, onRejected) { return new MyPromise((resolve, reject) => { // 1、调用then的时候status 没变的收将回掉放入到callback中 const callback = { onFulfilled, onRejected, resolve, reject } if (this.status === PENDING) { this.callbacks.push(callback) } else { // 2、调用then的时候,status如果已经改变了,直接执行 setTimeout(() => { this.handleCallback(callback) }, 0) } }) } catch(onRejected) { const onFulfilled = () => {} return new MyPromise((resolve, reject) => { const callback = { onFulfilled, onRejected, resolve, reject } if (this.status === PENDING) { this.callbacks.push(callback) } else { setTimeout(() => { this.handleCallback(callback) }, 0) } }) } finally(fn) { return this.then( value => MyPromise.resolve(fn()).then(() => value), reason => MyPromise.reject(fn()).then(() => {throw reason}) ) } excuteCallbacks() { this.callbacks.forEach(callback => { setTimeout(() => { this.handleCallback(callback) }, 0) }) } handleCallback(callback) { const { onFulfilled, onRejected, resolve, reject } = callback if (this.status === FULFILLED) { isFuntion(onFulfilled) ? resolve(onFulfilled(this.value)) : resolve(this.value) } if (this.status === REJECTED) { isFuntion(onRejected) ? reject(onRejected(this.reason)) : reject(this.reason) } } // 所有fulfilled才算成功 static all(list) { return new MyPromise((resolve, reject) => { const result = [] let count = 0 for(let i = 0; i < list.length; i ++) { list[i].then( val => { result[i] = val // 只有在每个promise都resolve之后count + 1 count ++ if (count === list.length) { resolve(result) } }, err => reject(err) ) } }) } // 每个promise都有值,不管是fulfilled还是rejected allSettled(list) { return new MyPromise((resolve) => { for (let i = 0; i < list.length; i++) { const result = [] let count = 0 list[i].then( val => { result[i] = { status: 'fulfilled', value: val } count ++ if (count === list.length) { resolve(result) } }, err => { result[i] = { status: 'rejected', reason: err } count ++ if (count === list.length) { resolve(result) } } ) } }) } // 返回先有结果的promise的值,不管是fulfilled还是rejected race(list) { return new MyPromise((resolve, reject) => { for (let i = 0; i < list.length; i ++) { list[i].then( val => resolve(val), err => reject(err) ) } }) } // 任意一个fulfilled就算成功, // 反过来说,所有的都rejected就是rejected any(list) { return new MyPromise((resolve, reject) => { const result = [] let count = 0 list[i].then( val => { resolve(val) }, err => { result[i] = err count ++ if (count === list.length) { reject(result) } } ) }) }}// const promise = new MyPromise((resolve, reject) => { resolve()}).then().then()