Promise.resolve()

Promise. Resolve (value) Can transform any value into value, which is a pity Promise. But if the value passed in is a Promise itself, it will be returned as is

Promise.resolve = function(value){
    // If it is a Promise, print it directly
    if(value instanceof Promise) {return value;
    }
    return new Promise(resolve= >resolve(value));
}
Copy the code

Promise.reject()

Similar to promise. resolve, promise. reject instantiates a Promise in the Rejected state. But unlike promise.resolve, if you pass a Promise object to promise.reject, that object becomes the value of the new Promise

Promise.reject = function(reason){
    return new Promise(resolve,reject)=>reject(reason));
}
Copy the code

Promise.all()

Promise.all rules:

  • All the promises passed in are fulfilled, and the new promises composed of their values will be returned, which is a big pity.
  • As long as one Promise is Rejected, a new Promise in the Rejected state is returned, and its value is the value of the first Rejected Promise.
  • As long as either Promise is pending, a new Promise in a pending state is returned;
Promise.all = function(promiseArr){
    let index =0,result = [];
    return new Promise((resolve,reject) = >{
        promiseArr.forEach((p,i) = >{
            Promise.resolve(p).then(value= >{
                index++;
                result[i] = value;
                if(index === promiseArr){ resolve(result); }},err= >{ reject(err); })})})}Copy the code

Promise.race()

Promise.race returns a new instance wrapped with the first Fulfilled or Rejected instance among all iterable instances.

Promise.race = function(promiseArr){
    return new Promise((resolve,reject) = >{
        promiseArr.forEach((p) = >{
            Promise.resolve(p).then((val) = >{
                resolve(val)
            },err= >{
                reject(err)
            })
        })
    })
}
Copy the code

Promise.allSettled()

Promise.allsettled rules:

  • This is a big pity. The states of all promises change, so the new state is fulfilled, and its value is an array. Each item of the array is an object composed of all Promise values and states.
  • If a Promise is pending, return a new instance whose state is pending;
Promise.allSettled = function(promiseArr){
    const result = []
    return new Promise((resolve,reject) = >{
        promiseArr.forEach((p) = >{
            Promise.resolve(p).then((val) = >{
                result.push({
                    value:val,
                    status:"fulfilled"
                })
                if(result.length === promiseArr.length){
                    resolve(result)
                }
            },err= >{
                result.push({
                    reason:err,
                    status:"rejected"
                })
                if(result.length === promiseArr.length){
                    resolve(result)
                }
            })
        })
    })
}
Copy the code

Promise.any()

Promise. Any rule:

  • An empty array or all promises are Rejected, and a new Promise with the status Rejected and the value AggregateError is returned.
  • As long as there is one big pity, the new instance of the first big pity will be returned
  • All other cases return a new instance of Pending
Promise.any = function (promiseArr){
    let index =0;
    return new Promise((resolve,reject) = >{
        promiseArr.forEach(p= >{
            Promise.resolve(p).then(val= >{
                resolve(val)
            })
        },err= >{
            index++;
            if(index === promiseArr.lenght){
                reject(new AggregateError("All Promises were rejected")}})})}Copy the code

Implementing the sleep function

const sleep = timeout= > {
    return new Promise((resolve,reject) = > {
        setTimeout(resolve,timeout)
    })
}
Copy the code

Generator

  1. Generator is a more modern asynchronous solution that supports coroutines at the JS language level
  2. The return value of Generator is an iterator
  3. This iterator needs to be manually tuned to next to perform yield line by line
  4. Next returns {value,done}, and value is the value of the expression following yield
  5. The yield statement itself does not return a value. The next call to next will return the value of the previous yield statement
  6. The Generator cannot execute automatically on its own. To do so, other schemes need to be introduced, and the CO module is a popular option
  7. Thunk and CO are similar in that they write a local method that calls Gen. Next, which itself is passed to a callback or promise success branch, and then continues to call the local method when the async ends. This iterates consistently until the iterator completes
  8. Async /await is a syntactic candy of Generator and autor, written and implemented in a similar way to the PROMISE mode of CO module.