-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (118 loc) · 3.32 KB
/
index.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
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
let id = 1
class Promise {
constructor(resolver) {
this.id = id++
this.status = PENDING
this.value = ''
this.onFulfilledCallback = []
this.onRejectedCallback = []
resolver(this.resolve.bind(this), this.reject.bind(this))
}
resolve(value) {
setTimeout(() => {
if (this.status === PENDING) {
// console.log('resolve')
this.status = FULFILLED
this.value = value
this.onFulfilledCallback && this.onFulfilledCallback.forEach(callback => callback(value))
} else {
console.log("status can't be changed to success!")
}
}, 0)
}
reject(value) {
setTimeout(() => {
if (this.status === PENDING) {
// console.log('reject')
this.status = REJECTED
this.value = value
this.onRejectedCallback && this.onRejectedCallback.forEach(callback => callback(value))
} else {
console.log("status can't be changed to failure!")
}
}, 0)
}
then(onFulfilled, onRejected) {
// console.log(this.id)
onFulfilled = typeof onFulfilled === "function" ? onFulfilled : value => value
onRejected = typeof onRejected === "function" ? onRejected : reason => reason
return new Promise((resolve, reject) => {
this.onFulfilledCallback.push(this.processThen(onFulfilled, resolve, reject))
this.onRejectedCallback.push(this.processThen(onRejected, resolve, reject))
})
}
processThen(callback, resolve, reject) {
return value => {
const nextValue = callback(value)
if (nextValue instanceof Promise) {
nextValue.then(val => resolve(val), reason => reject(reason))
} else {
resolve(nextValue)
}
}
}
finally(callback) {
return this.then(
value => Promise.resolve(callback()).then(() => value),
reason => Promise.resolve(callback()).then(() => { throw reason })
)
}
static promiseStatus(length, resolve) {
values = []
return (value, index) => {
values[index] = value
if (values.length === length) {
resolve(values)
}
}
}
static Resolve(value) {
return new Promise(resolve => resolve(value))
}
static Reject(value) {
return new Promise((resolve, reject) => reject(value))
}
static all(promises) {
return new Promise((resolve, reject) => {
const done = Promise.promiseStatus(promises.length, resolve)
promises.forEach((promise, index) => {
promise.then(value => {
done(value, index)
}, reject)
})
})
}
static race(promises) {
return new Promise((resolve, reject) => {
promises.forEach(promise => {
promise.then(resolve, reject)
})
})
}
}
// console.log(1)
new Promise((resolve, reject) => { reject(1) })
.then(function (value) {
console.log(value)
return 'suceess'
}, function (reason) {
console.log(reason)
// return 'failed'
return new Promise((resolve, reject) => { reject('demacia1') })
})
.then(function (value) {
console.log(value)
// return 'suceess1'
return new Promise(resolve => { resolve('demacia2') })
}, function (reason) {
console.log(reason)
return 'failed1'
})
.then(function (value) {
console.log(value)
}, function (reason) {
console.log(reason)
})