Appearance
promise 基本实现
作为 Promise 的调用,做出以下给哦那你的实现
javascript
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("ok");
}, 1000);
});
promise.then(
(data) => {
console.log(data, "success");
},
(err) => {
console.log(err, "fail");
}
);
promise.then(
(data) => {
console.log(data, "success1");
},
(err) => {
console.log(err, "fail");
}
);
promise.then(
(data) => {
console.log(data, "success2");
},
(err) => {
console.log(err, "fail");
}
);
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
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
首先 promise 的基本思路
创建状态
javascript
const PENDING = "PENDING"; // 等待中的状态
const FULFILLED = "FULFILLED"; // 完成状态
const REJECTED = "REJECTED"; // 失败状态
1
2
3
2
3
传入 executor 的方法
javascript
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("ok");
}, 1000);
});
// 这时候 new Promise 中的函数就是executor,只要给executor传入resolve、reject两个方法
1
2
3
4
5
6
2
3
4
5
6
设置返回值
javascript
this.status = PENDING; // 默认是等待
this.value = undefined; // 成功的原因
this.reason = undefined; // 失败的原因
1
2
3
2
3
通过 resolve、reject 两个方法改变状态
javascript
const resolve = (value) => {
// 如果是等待中泽修改
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
}
};
1
2
3
4
5
6
7
2
3
4
5
6
7
then 的使用
javascript
// then 中会返回 2 个方法,一个是完成、一个是失败
if (this.status === FULFILLED) {
// TODO...
onFULFILLED(this.value);
}
// 如果是等待中
// 这时候要先判断是否在等待中,如果已经有结果了直接返回结果
// 在.then中可以写入方法,
if (this.status === PENDING) {
// 如果我想拓展一些功能
this.onResolveCallbacks.push(() => {
// TODO...
onFULFILLED(this.value);
});
}
// 在构造函数中再去做数据的执行
const resolve = (value) => {
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
// ++ 执行方法的回调
this.onResolveCallbacks.forEach((cb) => cb());
}
};
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
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
将回调的方法收集,慢慢执行
javascript
this.onResolveCallbacks = []; // 例如成功的回调
const resolve = (value) => {
// 只有状态是 pending 的时候才可以改变状态
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
// 成功调用成功的回调
this.onResolveCallbacks.forEach((cb) => cb());
}
};
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
完整代码
javascript
const PENDING = "PENDING";
const FULFILLED = "FULFILLED";
const REJECTED = "REJECTED";
class Promise {
constructor(executor) {
this.status = PENDING; // 默认是等待
this.value = undefined; // 成功的原因
this.reason = undefined; // 失败的原因
this.onResolveCallbacks = [];
this.onRejectCallbacks = [];
// promise调用 then 的时候 可能状态依旧是 pending 那么我们需要将回调存起来
// 等待过一会调用resolve时候 onResolveCallbacks
// 等待过一会调用reject时候 onRejectCallbacks
const resolve = (value) => {
// 只有状态是 pending 的时候才可以改变状态
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
// 成功调用成功的回调
this.onResolveCallbacks.forEach((cb) => cb());
}
};
const reject = (reason) => {
if (this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
// 失败调用失败的回调
this.onRejectCallbacks.forEach((cb) => cb());
}
};
try {
// 调用excutor 会自动传入 resolve和reject
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
then(onFULFILLED, onRejectd) {
// 调用 then 的时候 已经确定了是成功还是失败
if (this.status === FULFILLED) {
// TODO...
onFULFILLED(this.value);
}
if (this.status === REJECTED) {
// TODO...
onRejectd(this.reason);
}
if (this.status === PENDING) {
// 如果我想拓展一些功能,就不行
// this.onResolveCallbacks.push(onFULFILLED)
// this.onRejectCallbacks.push(onRejectd)
this.onResolveCallbacks.push(() => {
// TODO...
onFULFILLED(this.value);
});
this.onRejectCallbacks.push(() => {
// TODO...
onRejectd(this.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
66
67
68
69