手写一个Promise(面试够用)

// 测试案例
new Promise((resolve, reject) => {
   setTimeout(() => resolve(new Promise(res => res(333))), 2000)
}).then(res => {
    console.log(res)
    return 222
}).then().then(res => {
    console.log(res);
    return new Promise(res => {
        setTimeout(() => { res(666) }, 1000)
    }).then(res => {
        console.log(res);
        return 999
    })
}).then(res => {
    console.log(res);
})
/*
 * 打印:
 * 333
 * 222
 * 666
 * 999
*/
// 上源码
;(function (global) {
	global.Promise = Promise
	// 定义promise三种状态
	const PENDING = 'pending'
	const FULFILLED = 'fulfilled'
	const REJECTED = 'rejected'

	function Promise(fn) {
		this.state = PENDING
		this.fulfilledQueue = []
		this.rejectedQueue = []
		try {
			fn(
				// 此处需注意this指向
				(res) => {
					this._resolve(res)
				},
				(err) => {
					this._reject(err)
				}
				// 或者this.resolve.bind(this), this.reject.bind(this))
			)
		} catch (e) {
			reject(e)
		}
	}

	Promise.prototype = {
		constructor: Promise,
		_resolve: function (value) {
			if (value instanceof Promise) {
				return value.then(
					// 此处需注意this指向
					(res) => {
						this._resolve(res)
					},
					(err) => {
						this._reject(err)
					}
				)
			}
			setTimeout(() => {
				if (this.state === PENDING) {
					this.state = FULFILLED
					this.value = value
					this.fulfilledQueue.forEach((fn) => fn(this.value))
				}
			})
		},
		_reject: function (value) {
			setTimeout(() => {
				if (this.state === PENDING) {
					this.state = REJECTED
					this.value = value
					this.rejectedQueue.forEach((fn) => fn(this.value))
				}
			})
		},
		then: function (fulfilled, rejected) {
			this.fulfilled =
				typeof fulfilled === 'function' ? fulfilled : (v) => v
			this.rejected =
				typeof rejected === 'function'
					? rejected
					: (err) => {
							throw err
					  }
			if (this.state === PENDING) {
				return new Promise((resolve, reject) => {
					this.fulfilledQueue.push(() => {
						try {
							this.value = this.fulfilled(this.value)
							resolve(this.value)
						} catch (error) {
							reject(error)
						}
					})
					this.rejectedQueue.push(() => {
						try {
							this.value = this.rejected(this.value)
							resolve(this.value)
						} catch (error) {
							reject(error)
						}
					})
				})
			}
			if (this.state === FULFILLED) {
				return new Promise((resolve, reject) => {
					setTimeout(() => {
						try {
							this.value = this.fulfilled(this.value)
							resolve(this.value)
						} catch (error) {
							reject(error)
						}
					})
				})
			}
			if (this.state === REJECTED) {
				return new Promise((resolve, reject) => {
					setTimeout(() => {
						try {
							this.value = this.rejected(this.value)
							resolve(this.value)
						} catch (error) {
							reject(error)
						}
					})
				})
			}
		},
	}
})(window)


  • 9
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值