Promise of doubt

Why was the promise resolve output value undefined

const test = new MPromise((resolve, reject) => { setTimeout(() => { resolve(111); }, 1000); }).then((value) => { console.log('then'); }); setTimeout(() => { console.log(test); }, 3000).Copy the code

Print result:

A. return undefined b. value undefined C. value undefined D. value undefined If you explicitly return a value, it is not undefined; Such as the return value.

Then returns a new Promise, so what’s the point of using an array to store the callback function when the original Promise was implemented?

When we ask this question, we assume that when we do chain calls.

At this point, each dot then returns a new promise, so led_callback_list is an empty array.

In this case, it really doesn’t make sense to store callbacks in arrays; you could just store them in a variable.

const test = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve(111);
    }, 1000);
}).then((value) => {
    
}).then(() => {

})
Copy the code

Answer: But there is another way that promises can be used, in which the promise instance is the same and the existence of the array makes sense

const test = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve(111);
    }, 1000);
})
test.then(() => {});
test.then(() => {});
test.then(() => {});
test.then(() => {});
Copy the code

Why do I print a promise in a catch callback that says the state is pending

const test = new Promise((resolve, reject) => { setTimeout(() => { reject(111); }, 1000); }). The catch ((reason) = > {the console. The log (" error "+" reason); console.log(test) }); setTimeout(() => { console.log(test); }, 3000).Copy the code

Print:

The answer:

  1. The catch function returns a new promise, and test is the new promise
  2. In the catch callback, when the promise is printed, the entire callback is not complete (so the state is pending), and the state changes only when the entire callback is complete
  3. The callback function of catch, if successfully executed, will change the state of this new Promise to depressing