Promise手写练习

2022/12/20

# Promise手写练习

class MyPromise {
    static PENDDING = "pendding";
    static FULLFILLING = "fullfilling";
    static REJECTED = "rejected";
    thenCallback = null;
    catchCallback = null;
    state = MyPromise.PENDDING;
    constructor(fn) {
        fn.call(null, this.resolve, this.reject);
        return this;
    }
    resolve = (res) => {
        if (this.state !== MyPromise.PENDDING) return;
        this.state = MyPromise.FULLFILLING;
        process.nextTick(() => this.thenCallback?.call(null, res));
    }
    reject = (reason) => {
        if (this.state !== MyPromise.PENDDING) return;
        this.state = MyPromise.REJECTED;
        process.nextTick(() => this.catchCallback?.call(null, reason));
    }
    then = (callback1, callback2) => {
        this.thenCallback = callback1;
        if (callback2) this.catchCallback = callback2;
        if (this.state === MyPromise.FULLFILLING) {
            process.nextTick(() => this.thenCallback?.call(null, res));
        }
        if (this.state === MyPromise.REJECTED) {
            process.nextTick(() => this.catchCallback?.call(null, reason));
        }
    }
    catch = () => {
        this.catchCallback = callback2;
        if (this.state === MyPromise.REJECTED) {
            process.nextTick(() => this.catchCallback?.call(null, reason));
        }
    }
}


new MyPromise((resolve, reject) => {
    setTimeout(() => resolve(1), 1000);
    setTimeout(() => reject(1), 2000);
}).then(res => {
    console.log(res);
}, (reason) => {
    console.log(reason);
})
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
上次更新: 6/13/2025