Roll your own Promise

[Musings]
今天在连江县,所谓的一县两省,台湾和福建省,都有居民,在这吃了很多海鲜,还喝酒了,中午和晚上都喝了点,实在是酒量不行,困得要死,影响了今天的进度。先写一版吧,迭代估计得明天再来了

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
type State = "pending" | "fulfilled" | "rejected";
class PromiseI {
private state: State;
private fnList: Map<string, Function>;
constructor(executer: (acc, rej) => void) {
executer(this.resolve.bind(this), this.reject.bind(this))
}

resolve(val: string) {
this.state = "fulfilled";
console.log("this is resolve:",val);
}

reject(val: string) {
this.state = "rejected";
console.log("this is reject:",val);
}

then(fn:Function) {
fn();
}
}
let p = new PromiseI(
(resolve,reject)=>{
resolve("test");
}
)

p.then(()=>{
console.log("here is callback then")
})