-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromise.js
139 lines (126 loc) · 3.27 KB
/
promise.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* prefix with `_method` or `_value` would be private method or value of Promise
*/
const states = {
PENDING: 'pending',
FULFILLED: 'fulfilled',
REJECTED: 'rejected'
}
const isThenable = o => typeof o === 'object' && typeof o.then === 'function'
function Promise(fn) {
if (!(this instanceof Promise)) throw Error('Promise must be called with new!')
if (!fn) throw Error(`${fn} is not a function!`)
this.state = states.PENDING
this.value = null
this._defers = []
try {
fn(this._resolve.bind(this), this._reject.bind(this))
} catch(e) {
if (this.state === states.PENDING) this._reject.call(this, e)
}
}
Promise.resolve = function(value) {
return new Promise(resolve => resolve(value))
}
Promise.reject = function(value) {
return new Promise((resolve, reject) => reject(value))
}
Promise.all = function (array) {
if (!Array.isArray(array)) {
throw Error('arguments must be array')
}
const allReadyCount = array.length
let readyCount = 0
const result = []
let rejected = false
return new Promise((resolve, reject) => {
const shouldResolve = () => !rejected && (++readyCount === allReadyCount) && resolve(result)
array.map(item => {
if (!isThenable(item)) item = Promise.resolve(item)
return item
}).forEach((item, index) => {
item.then(val => {
result[index] = val
shouldResolve()
}).catch(e => {
if (rejected) return
rejected = true
reject(e)
})
})
})
}
Promise.prototype.then = function (onFulfilled, onRejected) {
return new Promise((resolve, reject) => {
this._handle({
onFulfilled,
onRejected,
resolve,
reject
})
})
}
Promise.prototype.finally = function (onFinal) {
return new Promise((resolve, reject) => {
this._handle({
resolve,
reject,
onFinal: onFinal
})
})
}
Promise.prototype.catch = function (errorReject) {
return new Promise((resolve, reject) => {
this._handle({
resolve,
reject,
onRejected: errorReject
})
})
}
Promise.prototype._resolve = function (val) {
// handle with promise
if (isThenable(val)) {
val.then.call(val, this._resolve.bind(this), this._reject.bind(this))
return
}
if (this.state !== states.PENDING) return
this.state = states.FULFILLED
this.value = val
this._run()
}
Promise.prototype._reject = function (val) {
if (this.state !== states.PENDING) return
this.state = states.REJECTED
this.value = val
this._run()
}
Promise.prototype._handle = function (defer) {
if (this.state === states.PENDING) {
this._defers.push(defer)
} else {
const context = this
const { onFulfilled, onRejected, resolve, reject, onFinal } = defer
const isFulfilled = this.state === states.FULFILLED
const callback = isFulfilled ? onFulfilled : onRejected
if (!callback) {
setTimeout(function() {
onFinal && onFinal();
(isFulfilled ? resolve : reject)(context.value)
})
return
}
setTimeout(function() {
try {
resolve(callback(context.value))
} catch (e) {
reject(e)
}
})
}
}
Promise.prototype._run = function () {
// in the real word, would use micro task not task
this._defers.forEach((defer) => this._handle(defer))
}
export default Promise