-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise-fn.js
65 lines (57 loc) · 1.56 KB
/
promise-fn.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
// params: (resolve, reject) => void
/**
*
* @param {resolve, reject} executor
* ?? then 이후 반환되는 값은 "새로운" 프로미스인가?
* rejected 됐을 경우 then onRejection, catch 만 호출됨
* rejected 됐을 때 여러 then 체이닝이 있을 경우 가장 가까운 catch 문에 잡혀야함
*
* ?? prototype으로 상속 (constructor) 구현
*/
function MyPromise(executor) {
const status = {
PENDING: 'pending',
FULFILLED: 'fulfilled',
REJECTED: 'rejected',
}
this.status = status.PENDING
this.value = null
this.error = null
// private
// ** 중요! 콜스택이 아니라 큐에서 처리하게 함
const addToTaskQueue = (task) => setTimeout(task, 0)
this.then = function (onResolve, onRejection) {
const fulfilledTask = () => {
console.log('then')
onResolve(this.value)
}
switch (this.status) {
case status.PENDING: {
this.fulfilledFn = fulfilledTask
break
}
case status.FULFILLED: {
addToTaskQueue(fulfilledTask)
break
}
case status.REJECTED: {
break
}
}
}
this.resolve = function (value) {
this.status = status.FULFILLED
this.value = value
addToTaskQueue(this.fulfilledFn)
}
this.reject = function (error) {}
this.catch = function (callback) {
callback(this.error)
}
executor(this.resolve.bind(this), this.reject.bind(this))
}
const p = new MyPromise((resolve, reject) => {
console.log('executed')
setTimeout(() => resolve(2), 3000)
})
p.then((value) => console.log('resolved', value))