1. The first question

const promise = new Promise((resolve, reject) = > {
     console.log(1)
     resolve()
     console.log(2)
})
promise.then(() = > {
    console.log(3)})console.log(4)
Copy the code

Answer: 【 1,2,4,3 】

// console: [1, 2, 4, 3] 1. The new Promise parameter is executed synchronously. Then pushes the parameter function into the microtask queue, not directly executing 3. Outputs 4 (synchronous code), and then the event loop goes to the next round, outputs 3Copy the code

2. The second question

        var promise = new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve(1);
            }, 3000)
        })
        promise.then((a) = > {
            console.log(a)
            return Promise.resolve(2);
        }).then((n) = > {
            console.log("b")
            console.log(n)
        });
Copy the code

【1, B, 2】

Promise. Resolve is a Promise object that returns a new Promise object. Then is executed in the next event loopCopy the code

3. The third topic

        var promise = new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve(1);
            }, 3000)
        })
        promise.then(() = > {
            return 2
        }).then((n) = > {
            console.log(n)
        });
Copy the code

Answer: 【2】

Unlike the last problem, it doesn't have to wait for the next event loopCopy the code

4. The fourth question

        var promise = new Promise(function (resolve, reject) {
             setTimeout(function () {
                 resolve(1);
             }, 3000)
         })
         promise.then(2).then((n) = > {
             console.log(n)
         });
Copy the code

Answer: [1]

Then and catch expect the receiving function to take arguments, and if non-functions do, Promise penetration occurs, printing the return from the previous PromiseCopy the code

5. 5

        let a;
        const b = new Promise((resolve, reject) = > {
            console.log('promise1');
            resolve();
        }).then(() = > {
            console.log('promise2');
        }).then(() = > {
            console.log('promise3');
        }).then(() = > {
            console.log('promise4');
        });

        a = new Promise(async (resolve, reject) => {
            console.log(a);
            await b;
            console.log(a);
            console.log('after1');
            // resolve()
            await a
            resolve(true);
            console.log('after2');
        });

        console.log('end');
Copy the code

Promise1, undefined, end, promise2, promise3, promise4, (pending), after1, undefined

6. 6

        const promise = new Promise((resolve, reject) = > {
            resolve('success1');
            reject('error');
            resolve('success2');
        });

        promise
        .then((res) = > {
            console.log('then: ', res);
        })
        .catch((err) = > {
            console.log('catch: ', err);
        });
Copy the code

答案 : 【then:success1】

The promise state can only be changed onceCopy the code

7. Number 7

        Promise.resolve()
            .then(() = > {
                return new Error('error!!! ')
            })
            .then((res) = > {
                console.log('then: ', res)
            })
            .catch((err) = > {
                console.log('catch: ', err)
            })
Copy the code

【 THEN :Error: Error!!】

There is no Error or exception thrown, just a return of an object, even if it is an Error object, it will be a normal then chain call, and will not trigger a catch.Copy the code

8. 8

        const promise = new Promise((resolve, reject) = > {
            console.log(1);
            resolve();
            console.log(2);
            reject('error');
        })
        promise.then(() = > {
            console.log(3);
        }).catch(e= > console.log(e))
        console.log(4);
Copy the code

Answer: 【 1,2,4,3 】

A promise can only be modified once, so a catch is no longer executedCopy the code

9. 9

        const promise = new Promise((resolve, reject) = > {
             setTimeout(() = > {
                 console.log('once')
                 resolve('success')},1000)
         })
         promise.then((res) = > {
             console.log(res)
         })
         promise.then((res) = > {
             console.log(res)
         })
Copy the code

答案 : 【once, success, success】

The Promise constructor is executed only once, while the THEN method can be called multiple times, but the second time returns the result, with no asynchronous wait timeCopy the code

10. The first ten questions

        const p1 = () = > (new Promise((resolve, reject) = > {
            console.log(1);
            let p2 = new Promise((resolve, reject) = > {
                console.log(2);
                const timeOut1 = setTimeout(() = > {
                    console.log(3);
                    resolve(4);
                }, 0)
                resolve(5);
            });
            resolve(6);
            p2.then((arg) = > {
                console.log(arg);
            });

        }));
        const timeOut2 = setTimeout(() = > {
            console.log(8);
            const p3 = new Promise(reject= > {
                reject(9);
            }).then(res= > {
                console.log(res)
            })
        }, 0)
        p1().then((arg) = > {
            console.log(arg);
        });
        console.log(10);
Copy the code

【1,2,10, 5,6, 8,9, 3】

1. Execute the synchronization code and print 1, 2, 10. 2. Since the time of the two timers is the same, the global timer is executed first. After entering the timer, 8,9, and 4 are printed. 5. A promise can only be modified once, so 4 cannot be printedCopy the code