手写promise源码

//1.Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去, 执行器会立即执行
//2.Promise 中有三种状态 分别为成功 fulfilled  失败 rejected  等待 pending
// pending  -> fulfilled
// pending  -> rejected
// 一旦状态决定就不可更改
//3.resolve 和 reject 函数是用来更改状态的
// resolve:fulfilled
// reject“ rejected
//4.then方法内部做的事情就是判断状态,如果状态是成功 调用成功的回调函数 如果是失败 调用失败的方法
// then方法是定义在原型对象中的方法
//5.then成功回调有一个参数 表示成功之后的值 then失败回调后有一个参数 代表失败的原因
//6.then方法可以被多次调用 成功或失败后按顺序依次执行多次回调
//7.then方法是可以被链式调用的,后面then方法的回调函数拿到值的是上一个then方法的回调函数的返回值

const PENDING = 'pending'; //等待
const FULFILLED = 'fulfilled'; //成功
const REJECTED = 'rejected'; //失败

class MyPromise {
	constructor(executor){
		 try{
			executor(this.resolve, this.reject)
		 } catch(e){
           	this.reject(e);	
		 }
	}
	// promise 状态
	status = PENDING; 
	// 成功之后的值
	value = undefined;
	// 失败的原因
	reason = undefined;
	// 成功回调
	successCallback = [];
	// 失败回调
	failCallback = [];
	resolve = value =>{
		// 如果状态不是等待 阻止程序向下进行
		if(this.status !== PENDING) return;
		// 将状态更改为成功
		this.status = FULFILLED;
		// 保存成功之后的值
		this.value = value;
		// 判断成功回调是否存在,如果存在 则调用
		while(this.successCallback) this.successCallback.shift()(this.value);
	}
	reject = reason =>{
		// 如果状态不是等待 阻止程序向下进行
		if(this.status !== PENDING) return;
	    // 将状态更改为失败
		this.status = REJECTED;
		// 保存失败后的原因
		this.reason = reason;
		// 判断失败回调是否存在,如果存在 则调用
		while(this.failCallback) this.failCallback.shift()(this.reason);
	}
	then = (successCallback, failCallback) =>{
		// 将then方法的参数变为可选参数
		 successCallback = successCallback ? successCallback : value => value
		 failCallback = failCallback ? failCallback : reason => { throw reson };
		let promise2 = new MyPromise((resolve, reject)=>{
			if(this.status === FULFILLED){
				// 变成异步代码,得到promise2
				setTimeout(() =>{
					try{
						let x = successCallback(this.value)
						resolvePromise(promise2, x, resolve, reject)
					}catch(e){
						reject(e);
					}
				})
			}else if(this.status === REJECTED){
				setTimeout(() =>{
					try{
						let x = failCallback(this.reason)
						resolvePromise(promise2, x, resolve, reject)
					}catch(e){
						reject(e);
					}
				})
			}else{
				// 等待
				// 将成功回调和失败回调存储起来
				this.successCallback.push(()=>{
					setTimeout(() =>{
						try{
							let x = successCallback(this.value)
							resolvePromise(promise2, x, resolve, reject)
						}catch(e){
							reject(e);
						}
					})
				});
				this.failCallback.push(()=>{
					setTimeout(() =>{
						try{
							let x = failCallback(this.reason)
							resolvePromise(promise2, x, resolve, reject)
						}catch(e){
							reject(e);
						}
					})
				});
			}
		});
		return promise2;
	}
	finally(callback){
		return this.then(value => {
			return MyPromise.resolve(callback().then(() => value));
			callback();
		}, reason =>{
			return MyPromise.resolve(callback().then(() => { throw reason }));
			callback();
		})
	}
	catch(failCallback){
		return this.then(undefined, failCallback)
	}
	static all (array) {
		let result = [];
		let index = 0;
		return new myPromise((resolve, reject)=>{
			function addData(key, value){
				result[key] = value;
				index++;
				if(index === array.length){
					resolve(result);
				}
			}
			for(let i = 0; i < array.length; i++){
				let current = array[i];
				if(current instanceof MyPromise){
					// promise对象
					current.then(value => addData(i, value), reason =>reject(reason))
				}else{
					// 普通值
					addData(i, array[i])
				}
			}
		})
	}
	static resolve(value){
		if(value instanceof MyPromise) return value;
		return new MyPromise(resolve => resolve(value));
	}
	static 
}

function resolvePromise(promise2, x, resolve, reject){
	if(promise2 === x){
		return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
	}
	// 判断 x 的值是普通值还是promise对象
	// 如果是普通值 直接调用resolve
	// 如果是promise对象 查看promise对象返回的结果
	// 再根据promise对象返回的结果,决定调用resolve还是调用reject
	if(x instanceof MyPromise){
		// x.then(value => resolve(value), reason=>reject(reason));
		x.then(resolve, reject);
	}else{
		resolve(x);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值