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
class MyPromise {
constructor(fn) {
fn(value=>this.cb?.(value))
}
then(fn) {
return new MyPromise(res=>this.cb = val=>{
const thenCb = fn(val)
thenCb instanceof MyPromise ? thenCb.then(res) : res(thenCb)
})
}
}

new MyPromise((resolve)=>{
setTimeout(()=>{
resolve(1)
}, 1000)
}
).then((res)=>{
console.log(res)
return new MyPromise((resolve)=>{
setTimeout(()=>{
resolve(2)
}, 1000)
})
}
).then(res=>{
console.log(res)
})