关于promise

promise实现

以下为手写promise,实现的比较粗略
promise作为构造函数实现

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>promise的实现</title>
</head>
<body>
<script>

// let a = new Promise((resolve, reject) => {
// console.log(123);
// setTimeout(function () {
// resolve('hhh')
// }, 0)
//
// });
// a.then(res => {
// console.log(1233);
// console.log(res)
// }, rej => {
// console.log(rej)
// })
// Promise.all([1,2,3]).then(val => {
// console.log(val)
// })
// Promise.race([1,2,3]).then(val => {
// console.log(val)
// })

console.log('----------------------');
function promise(executor) {
let self = this;
self.status = 'pending';
self.data = null;
self.callbacks = {};

function resolve(data) {
if (self.status !== 'pending') return;
self.status = 'resolved';
self.data = data;
if (self.callbacks.onResolved) {
self.callbacks.onResolved(data)
}
}
function reject(data) {
if (self.status !== 'pending') return;
self.status = 'rejected';
self.data = data;
if (self.callbacks.onRejected) {
self.callbacks.onRejected(data)
}
}
executor(resolve, reject)
}
promise.prototype.then = function(onResolved, onRejected) {
let self = this;
onResolved = typeof onResolved === 'function' ? onResolved : value => value;
onRejected = typeof onRejected === 'function' ? onRejected : reason => {throw reason};
if (self.status === 'resolved') {
queueMicrotask(() => {
handle(onResolved)
})
} else if (self.status === 'rejected') {
queueMicrotask(() => {
handle(onRejected)
})
} else if (self.status === 'pending') {
self.callbacks = {
onResolved: () => {
queueMicrotask(() => {
handle(onResolved)
})
},
onRejected: () => {
queueMicrotask(() => {
handle(onRejected)
})
}
}
}
function handle(callback) {
return new promise((resolve, reject) => {
resolve(callback(self.data))
})
}
};
promise.resolve = function(val) {
return new promise(resolve => {
resolve(val)
})
}

promise.race = function (args) {
return new promise((resolve, reject) => {
args.forEach(item => {
promise.resolve(item).then(val => {
resolve(val)
}, val => {
reject(val)
})
})
})

};
promise.all = function (args) {
let results = [];
let num = 0;
let total = args.length
return new promise((resolve, reject) => {
args.forEach((item, index) => {
promise.resolve(item).then(val => {
results[index] = val;
num++;
if (num === total) {
resolve(results)
}
}, val => {
reject(val)
})
})
})
};

let b = new promise((resolve, reject) => {
console.log(123)
setTimeout(function () {
resolve('hhh')
}, 0)
});
b.then(res => {
console.log(222);
console.log(res)
});

promise.race(['r','a','c', 'e']).then(val => {
console.log(val)
})

promise.all(['a','l','l']).then(val => {
console.log(val)
})
</script>
</body>
</html>

参考实现方式

promise A+ 规范

promise面试题