Class 版
class mPromise {
constructor(executor) {
this.PromiseState = "pending";
this.PromiseResult = null;
this.callbacks = [];
const _this = this;
function resolve(data) {
if (_this.PromiseState !== "pending") return;
_this.PromiseState = "fulfilled";
_this.PromiseResult = data;
setTimeout(() => {
_this.callbacks.forEach((item) => {
item.onResolved(data);
});
});
}
function reject(data) {
if (_this.PromiseState !== "pending") return;
_this.PromiseState = "rejected";
_this.PromiseResult = data;
setTimeout(() => {
_this.callbacks.forEach((item) => {
item.onRejected(data);
});
});
}
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
then(onResolved, onRejected) {
const _this = this;
if (typeof onRejected !== "function") {
onRejected = (err) => {
throw err;
};
}
if (typeof onResolved !== "function") {
onResolved = (val) => val;
}
return new mPromise((resolve, reject) => {
function callback(onResult) {
try {
const result = onResult(_this.PromiseResult);
if (result instanceof mPromise) {
result.then(
(val) => {
resolve(val);
},
(err) => {
reject(err);
}
);
} else {
resolve(result);
}
} catch (e) {
reject(e);
}
}
if (this.PromiseState === "fulfilled") {
setTimeout(() => {
callback(onResolved);
});
}
if (this.PromiseState === "rejected") {
setTimeout(() => {
callback(onRejected);
});
}
if (this.PromiseState === "pending") {
this.callbacks.push({
onResolved: () => {
callback(onResolved);
},
onRejected: () => {
callback(onRejected);
},
});
}
});
}
catch(onRejected) {
return this.then(undefined, onRejected);
}
static resolve(value) {
return new mPromise((resolve, reject) => {
if (value instanceof mPromise) {
value.then(
(val) => {
resolve(val);
},
(err) => {
reject(err);
}
);
} else {
resolve(value);
}
});
}
static reject(err) {
return new mPromise((resolve, reject) => {
reject(err);
});
}
static all(promises) {
return new mPromise((resolve, reject) => {
let count = 0;
const arr = [];
promises.forEach((promise, i) => {
promise.then(
(val) => {
arr[i] = val;
count++;
if (count === promises.length) {
resolve(arr);
}
},
(err) => {
reject(err);
}
);
});
});
}
static race(promises) {
return new mPromise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(
(val) => {
resolve(val);
},
(err) => {
reject(err);
}
);
});
});
}
}

- 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
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
function 版
function mPromise(executor) {
this.PromiseState = "pending";
this.PromiseResult = null;
this.callbacks = [];
const _this = this;
function resolve(data) {
if (_this.PromiseState !== "pending") return;
_this.PromiseState = "fulfilled";
_this.PromiseResult = data;
setTimeout(() => {
_this.callbacks.forEach((item) => {
item.onResolved(data);
});
});
}
function reject(data) {
if (_this.PromiseState !== "pending") return;
_this.PromiseState = "rejected";
_this.PromiseResult = data;
setTimeout(() => {
_this.callbacks.forEach((item) => {
item.onRejected(data);
});
});
}
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
mPromise.prototype.then = function (onResolved, onRejected) {
const _this = this;
if (typeof onRejected !== "function") {
onRejected = (err) => {
throw err;
};
}
if (typeof onResolved !== "function") {
onResolved = (val) => val;
}
return new mPromise((resolve, reject) => {
function callback(onResult) {
try {
const result = onResult(_this.PromiseResult);
if (result instanceof mPromise) {
result.then(
(val) => {
resolve(val);
},
(err) => {
reject(err);
}
);
} else {
resolve(result);
}
} catch (e) {
reject(e);
}
}
if (this.PromiseState === "fulfilled") {
setTimeout(() => {
callback(onResolved);
});
}
if (this.PromiseState === "rejected") {
setTimeout(() => {
callback(onRejected);
});
}
if (this.PromiseState === "pending") {
this.callbacks.push({
onResolved: () => {
callback(onResolved);
},
onRejected: () => {
callback(onRejected);
},
});
}
});
};
mPromise.prototype.catch = function (onRejected) {
return this.then(undefined, onRejected);
};
mPromise.resolve = function (value) {
return new mPromise((resolve, reject) => {
if (value instanceof mPromise) {
value.then(
(val) => {
resolve(val);
},
(err) => {
reject(err);
}
);
} else {
resolve(value);
}
});
};
mPromise.reject = function (err) {
return new mPromise((resolve, reject) => {
reject(err);
});
};
mPromise.all = function (promises) {
return new mPromise((resolve, reject) => {
let count = 0;
const arr = [];
promises.forEach((promise, i) => {
promise.then(
(val) => {
arr[i] = val;
count++;
if (count === promises.length) {
resolve(arr);
}
},
(err) => {
reject(err);
}
);
});
});
};
mPromise.race = function (promises) {
return new mPromise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(
(val) => {
resolve(val);
},
(err) => {
reject(err);
}
);
});
});
};

- 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
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171