手写Promise(一)

思路梳理

1、promise是一个类,执行该类的时候,需要传递一个执行器

class MyPromise {
	construntor(executor) {
		executor();
	}
}

2、promise有三种状态:pending(执行中)、fulfilled(执行成功)、rejected(执行失败)

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
	status = PENDING; // 实例属性status,初始值pending
	construntor(executor) {
		executor();
	}
}

3、resolve和reject是更改状态的,且状态只能由pending->fulfilled,pending->rejected两种

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
	status = PENDING; // 实例属性status,初始值pending
	construntor(executor) {
		executor(this.resolve, this.reject);
	}
	resolve = () => {
		if (this.status !== PENDING) return;
		this.status = FULFILLED;
	}
	reject = () => {
		if (this.status !== PENDING) return;
		this.status = REJECTED;
	}
}

4、then方法,判断其状态,根据状态不同,执行不同的回调函数

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
	status = PENDING; // 实例属性status,初始值pending
	construntor(executor) {
		executor(this.resolve, this.reject);
	}
	resolve = () => {
		if (this.status !== PENDING) return;
		this.status = FULFILLED;
	}
	reject = () => {
		if (this.status !== PENDING) return;
		this.status = REJECTED;
	}
	// start step 4
	then = (successCallback, failCallback) => {
		if (this.status === FULFILLED) {
			successCallback();
		} else if (this.status === REJECTED) {
			failCallback()
		}
	}
	// end step 4
}

5、then方法执行回调函数,成功时,需要返回成功的值;失败时,需要返回失败原因

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
	status = PENDING; // 实例属性status,初始值pending
	// start step 5
	value = undefined;	// 状态成功时,保存成功的值
	reason = undefined; // 状态失败时,保存失败的原因
	// end step 5
	
	construntor(executor) {
		executor(this.resolve, this.reject);
	}
	resolve = value => { // step 5 value
		if (this.status !== PENDING) return;
		this.status = FULFILLED;
		// start step 5
		this.value = value;
		// end step 5
	}
	reject = reason => { // step 5 reason
		if (this.status !== PENDING) return;
		this.status = REJECTED;
		// start step 5
		this.reason = reason;
		// end step 5
	}
	then = (successCallback, failCallback) => {
		if (this.status === FULFILLED) {
			successCallback(this.value); // step 5 successCallback() -> successCallback(this.value)
		} else if (this.status === REJECTED) {
			failCallback(this.reason); // step 5 failCallback() -> failCallback(this.reason)
		}
	}
}

6、关于resove和reject异步的情况
下述情况中,执行器中加入了延时函数,由于js时按顺序执行,在遇到setTimeout时,并不会等待其执行,所以在执行then方法时,得到的状态仍为pending

const promise = new MyPromise((resolve, reject) => {
	setTimeout(() => {
		resolve('success');
		// reject('fail');
	}, 3000);
})

promise.then(value => {
	console.log(value)
})

故,需要考虑then方法中,状态为pending时,需要存储成功回调和失败回调

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
	status = PENDING; // 实例属性status,初始值pending
	value = undefined;	// 状态成功时,保存成功的值
	reason = undefined; // 状态失败时,保存失败的原因
	// start step 6
	successCallback = undefined; // 保存成功回调
	failCallback = undefined;	// 保存失败回调
	// end step 6

	construntor(executor) {
		executor(this.resolve, this.reject);
	}
	resolve = value => {
		if (this.status !== PENDING) return;
		this.status = FULFILLED;
		this.value = value;
		// start step 6
		this.successCallback && this.successCallback(this.value);
		// end step 6
	}
	reject = reason => {
		if (this.status !== PENDING) return;
		this.status = REJECTED;
		this.reason = reason;
		// start step 6
		this.failCallback && this.failCallback(this.value);
		// end step 6
	}
	then = (successCallback, failCallback) => {
		if (this.status === FULFILLED) {
			successCallback(this.value);
		} else if (this.status === REJECTED) {
			failCallback(this.reason);
		} else { // start step 6
			this.successCallback = successCallback;
			this.failCallback = failCallback;
		} // end step 6
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值