-
Notifications
You must be signed in to change notification settings - Fork 0
/
myPromise.js
65 lines (61 loc) · 1.79 KB
/
myPromise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 等待
const PENDING = 'pending'
// 成功
const FULFILLED = 'fulfilled'
// 失败
const REJECTED = 'rejected'
/**
* 传入值否为函数
* @param {*} value 传入value
* @returns boolean
*/
const isFunction = value => typeof value === 'function'
/**
* 传入值类型是否为对象
* @param {*} value 传入值
* @returns boolean
*/
const isObject = value => Object.prototype.toString.call(value) === '[object Object]'
class MyPromise {
/**
* 初始化函数
* @param { Function } executor 执行函数
*/
constructor(executor) {
// 保存异步操作执行结果
this.result = null
// 记录当前Promise状态
this.state = PENDING
/**
* 异步操作成功时调用
* @param {*} value 操作成功结果
* @returns void
*/
const reslove = value => {
// 若当前状态已经被修改则不可修改状态
if (this.state !== PENDING) return
// 修改状态为执行成功
this.state = FULFILLED
// 保存操作成功结果
this.result = value
}
/**
* 异步操作失败时调用
* @param {*} reason 错误信息
* @returns void
*/
const reject = reason => {
if (this.state !== PENDING) return
// 修改状态为执行失败
this.state = REJECTED
// 保存执行失败错误信息
this.result = reason
}
// 执行外部传入参数
executor(reslove, reject)
}
}
const p = new MyPromise((resolve, reject) => {
Math.floor(Math.random() * 10) > 4 ? resolve('success') : reject('fail')
})
console.log('MyPromise: ', p)