Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b74f377

Browse files
committed
feat: 最简单的promise异步调用链
1 parent 617edec commit b74f377

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

promise-easy.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
/**
22
* 仅实现异步链式调用的简化版
33
*/
4-
54
function Promise(excutor) {
65
var self = this
7-
self.status = "pending" // Promise当前的状态
8-
self.data = undefined // Promise的值
96
self.onResolvedCallback = [] // Promise resolve时的回调函数集,因为在Promise结束之前有可能有多个回调添加到它上面
107

118
function resolve(value) {
12-
setTimeout(() => {
13-
if (self.status === "pending") {
14-
self.status = "resolved"
15-
self.data = value
16-
self.onResolvedCallback.forEach(callback => callback(value))
17-
}
18-
})
9+
self.data = value
10+
self.onResolvedCallback.forEach(callback => callback(value))
1911
}
2012

2113
excutor(resolve.bind(self))
@@ -49,12 +41,4 @@ new Promise(resolve => {
4941
}, 500)
5042
})
5143
})
52-
.then(res => {
53-
console.log(res)
54-
return new Promise(resolve => {
55-
setTimeout(() => {
56-
resolve(3)
57-
}, 500)
58-
})
59-
})
6044
.then(console.log)

0 commit comments

Comments
 (0)