Promise 方法
- all :成功的时候返回的是一个结果数组,失败的时候则返回最先被reject失败状态的值
- allthrottle:返回一个结果数组,包含成功、失败
- any:只要其中的一个 promise 成功,就返回那个已经成功的 promise
- rance:哪个结果获得的快,就返回那个结果,不管结果本身是成功状态还是失败状态
- resolve、 reject
const PENDING = "PENDING";
const FULFILLED = "FULFILLED";
const REJECTED = "REJECTED";
class MyPromise {
constructor(executor) {
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
this.onFulfilledCallbacks = [];
this.onRejectedCallbacks = [];
const resolve = (value) => {
if (this.status == PENDING) {
this.value = value;
this.status = FULFILLED;
this.onFulfilledCallbacks.forEach(fn => fn());
}
}
const reject = (value) => {
if (this.status == PENDING) {
this.reason = value;
this.status = REJECTED;
this.onRejectedCallbacks.forEach(fn => fn());
}
}
try {
executor(resolve, reject);
} catch (error) {
reject(error)
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === "function"
? onFulfilled
: (value) => value;
onRejected = typeof onRejected === "function"
? onRejected
: (reason) => { throw reason };
let promise2 = new MyPromise((resolve, reject) => {
if (this.status == FULFILLED) {
setTimeout(() => {
try {
let res = onFulfilled(this.value)
resolvePromise(promise2, res, resolve, reject)
} catch (error) {
reject(error);
}
}, 0);
}
if (this.status == REJECTED) {
setTimeout(() => {
try {
let res = onRejected(this.reason)
resolvePromise(promise2, res, resolve, reject)
} catch (error) {
reject(error);
}
}, 0);
}
if (this.status == PENDING) {
this.onFulfilledCallbacks.push(() => {
setTimeout(() => {
try {
let res = onFulfilled(this.value)
resolvePromise(promise2, res, resolve, reject)
} catch (error) {
reject(error);
}
}, 0);
})
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try {
let res = onRejected(this.reason)
resolvePromise(promise2, res, resolve, reject)
} catch (error) {
reject(error);
}
}, 0);
})
}
})
return promise2;
}
catch(errorCallback) {
return this.then(null, errorCallback)
}
resolve(value) {
return new MyPromise((resolve, reject) => {
resolve(value);
})
}
reject(reason) {
return new MyPromise((resolve, reject) => {
reject(reason);
})
}
all(promiseArray) {
if (!Array.isArray(promiseArray)) {
throw new Error("请传入PromiseArray")
}
return new MyPromise((resolve, reject) => {
try {
const resultArray = [];
const length = promiseArray.length;
for (let i = 0; i < length; i++) {
promiseArray[i].then(data => {
resultArray.push(data);
if (resultArray.length === length) {
resolve(resultArray)
}
}, reject)
}
} catch (e) {
reject(e)
}
})
}
race(promiseArray) {
if (!Array.isArray(promiseArray)) {
throw new Error("请传入PromiseArray")
}
return new MyPromise((resolve, reject) => {
try {
const length = promiseArray.length;
for (let i = 0; i < length; i++) {
promiseArray[i].then(resolve, reject);
}
}
catch (e) {
reject(e)
}
})
}
}
function resolvePromise(promise2, res, resolve, reject) {
if (promise2 == res) {
return reject(new TypeError('Changing ...'))
}
if ((typeof res === "object" && res !== null) || (typeof res === "function")) {
try {
let then = res.then;
let called = false;
if (typeof then === "function") {
then.call(res, (y) => {
if (called) return;
called = true;
resolvePromise(promise2, y, resolve, reject)
}, (r) => {
if (called) return;
called = true;
reject(r)
})
} else {
resolve(res);
}
} catch (error) {
if (called) return;
called = true;
reject(error)
}
} else {
resolve(res);
}
}
const p1 = new MyPromise((resolve, reject) => {
reject(1)
})
const p2 = p1.then((res) => {
return res + 1;
}, err => {
return err + 2
})
p2.then((res) => {
console.log("res", res);
}, err => {
console.log('err', err);
})