This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Official explanation:

The promise.all () method accepts an iterable of promises. Array, Map, and Set are all ES6 iterable types), and only one Promise instance is returned. The result of the resolve callbacks to all of those entered promises is an Array. The Promise’s resolve callback is executed when all the incoming Promise’s resolve callbacks have ended, or when no promises are left in the input iterable. Its Reject callback execution throws an error as soon as any incoming promise’s Reject callback is executed or an invalid promise is entered, and reject is the first error message thrown.

Let’s just implement it a little bit

Implement a simple version of promise.all

Promise.all is a static method

Static methods: Static method calls are made directly on the class and cannot be called on an instance of the class. (Class or constructor)

Instance methods: Cannot be accessed directly through a class, but must be accessed through an instance declared by that class

    Promise.all = function() {},// Static methods defined directly on a constructor or class

    Promise.prototype.all = function() {}, // Instance method, defined on Prototype

Copy the code

The promise. all argument is an Array type that returns a Promise

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

Process the input parameter and get the result

Here are a few things to note:

  • The incoming parameter is an iterable type of promise, but you still need to handle cases where the incoming parameter is not a promise type

  • The return result is an X (of type Promise)

  • The x. Chen method retrieves the result of all the processing and is also an iterable

  • The X. catch method retrieves the error

  • The x. Chen callback will not return until all incoming parameters are resolved

  • The order in which all the results are processed is the same as the order in which incoming promises are made

    Promise.all = function(arr) {
        return new Promise((resolve, reject) = > {
            let res = [];
            let count = 0;
            for(let i = 0; i < arr.length; i++) {
                Promise.resolve(arr[i])
                    .then((value) = > {
                        res[i] = value;
                        count++;
                        if(count === arr.length) {
                            resolve(res);
                        }
                    })
                    .catch((reason) = > {
                        reject(reason)
                    })
            }
        })
    }
Copy the code

There are often mistakes, or places to look

  • res[i] = value; It is often written as res.push(value), which results in results returned in a different order than expected

  • Count === arr.length will be miswritten as res.length === arr.length. If arr[1] = ‘XXX’, then the length of arr is 2, not 1, even if arr[0] has not been assigned.

  • Arr [I] needs to be wrapped in promise.resolve to handle some exceptions