(function () {
const PENDING = 'pending';
const RESOLVED = 'resolved';
const REJECTED = 'rejected';
// 1. Define three states to indicate how far a Promise's asynchronous task has progressed
function MyPromise(fn) {
The fn argument in MyPromise is a custom function that needs to be passed in by the user (this function accepts resolve and reject).
// 3. There are also two internal functions in MyPromise: resolve and reject, where resolve represents the function that should be called when the user's asynchronous task succeeds, and reject represents the function that should be called when the user's asynchronous task fails.
const that = this;
that.state = PENDING;
// 4. The initial state is pending
that.resolvedCallbackArray = [];
that.rejectedCallbackArray = [];
// 5. For chained calls to THEN, represent two callback functions as arrays of functions
function resolve(value) {
that.state = RESOLVED;
// 6. Change the state when resolve is called
that.resolvedCallbackArray.forEach(cbFn= > cbFn(value));
// // 7. Iterate over the user's callback function and report the result
}
function reject(value) {
that.state = REJECTED;
// reject Modifies state when called
that.rejectedCallbackArray.forEach(cbFn= > cbFn(value));
// // iterates through the user's callback function, telling the result
}
fn(resolve, reject);
}
MyPromise.prototype.then = function(onFulfilled, onRejected) {
// 8. Verify the parameters and check the status. Then is used as an example to check whether the parameters passed by the user are functions
if(typeofonRejected ! = ='function') {
onRejected = v= > v;
}
if(typeofonFulfilled ! = ='function') {
onFulfilled = v= > { throw r };
}
const that = this;
if (that.state === PENDING) {
that.resolvedCallbacks.push(onFulfilled)
that.rejectedCallbacks.push(onRejected)
}
if (that.state === RESOLVED) {
onFulfilled(that.value)
}
if (that.state === REJECTED) {
onRejected(that.value)
}
}
new MyPromise((resolve, reject) = > {
setTimeout(() = > {
resolve('You saw me again.');
}, 2000)
}).then(value= > {
console.log('First time', value);
}).then(value= > {
console.log('The second time', value); }); }) ();Copy the code
Promise. all: Returns an array of results on success, while failure returns the value of the first rejected state.
let p1 = new Promise((resolve, reject) = > {
resolve('It worked')})let p2 = new Promise((resolve, reject) = > {
resolve('success')})let p3 = Promse.reject('failure')
Promise.all([p1, p2]).then((result) = > {
console.log(result) //[' success', 'success']
}).catch((error) = > {
console.log(error)
})
Promise.all([p1,p3,p2]).then((result) = > {
console.log(result)
}).catch((error) = > {
console.log(error) // Failed, type 'failed'
})
Copy the code
Promise.race([P1, P2, p3]) returns the fastest result, regardless of whether the result itself is a success or failure state
let p1 = new Promise((resolve, reject) = > {
setTimeout(() = > {
resolve('success')},1000)})let p2 = new Promise((resolve, reject) = > {
setTimeout(() = > {
reject('failed')},500)})Promise.race([p1, p2]).then((result) = > {
console.log(result)
}).catch((error) = > {
console.log(error) // 打开的是 'failed'
})
Copy the code