forked from FrankFang/promise-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.ts
65 lines (61 loc) · 1.57 KB
/
promise.ts
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
class Promise2 {
state = "pending";
callbacks = [];
resolve(result) {
if (this.state !== "pending") return;
this.state = "fulfilled";
nextTick(() => {
// 遍历 callbacks,调用所有的 handle[0]
this.callbacks.forEach(handle => {
if (typeof handle[0] === "function") {
handle[0].call(undefined, result);
}
});
});
}
reject(reason) {
if (this.state !== "pending") return;
this.state = "rejected";
nextTick(() => {
// 遍历 callbacks,调用所有的 handle[0]
this.callbacks.forEach(handle => {
if (typeof handle[1] === "function") {
handle[1].call(undefined, reason);
}
});
});
}
constructor(fn) {
if (typeof fn !== "function") {
throw new Error("我只接受函数");
}
fn(this.resolve.bind(this), this.reject.bind(this));
}
then(succeed?, fail?) {
const handle = [];
if (typeof succeed === "function") {
handle[0] = succeed;
}
if (typeof fail === "function") {
handle[1] = fail;
}
this.callbacks.push(handle);
// 把函数推到 callbacks 里面
return undefined;
}
}
export default Promise2;
function nextTick(fn) {
if (process !== undefined && typeof process.nextTick === "function") {
return process.nextTick(fn);
} else {
var counter = 1;
var observer = new MutationObserver(fn);
var textNode = document.createTextNode(String(counter));
observer.observe(textNode, {
characterData: true
});
counter = counter + 1;
textNode.data = String(counter);
}
}