background

motivation

I have encountered these before when WRITING code and made mistakes. Now I read the document and have a feeling. I hereby record this.

The environment

The node v14.7.0

tips

1. Resolve and reject cannot be used as return

I didn’t know this before, but I had a bug when I used return

Resolve to prove

Code:

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

Print result:

1 2 3Copy the code

Reject to prove

Ditto, slightly

2. new Promise(…) It can run without writing then/catch

Code:

new Promise((res,rej) = > {
    console.log(1);
});
console.log(2); / / must pay attention to
Copy the code

Print result:

2
1
Copy the code

3. Then /catch skip and pass values

Scenario: Encountered earlier when doing multiple steps to set up multiple error handling. For example, to implement this requirement:

Promise a callback interface. The catch (e = > {interface error handling Can not run behind}). Then (res = > {used in processing the data, for parameters to callback interface 2}). The catch (e = > error handling} {interface 2)Copy the code

Skip the catch

The second then of.then.catch. Then could not get the value of the first THEN. Now it turns out it can.

Conclusion: Not only does the run cross, but the value also crosses (if true)

Code:

constPrint and pass the value =(v) = > {
  console.log(v);
  return v;
};
new Promise((res, rej) = > {
  console.log(1);
  res(2);
})
  .catch((e) = > console.log(e)).then(print and pass values).catch((e) = > console.log(e)).then(print and pass values);Copy the code

Print result:

1
2
2
Copy the code

I can’t jump when I catch you.

I understand it in terms of the result: catch catches an error and eats it, and for the rest of the code, there are no more errors. So unless a catch throws an Error, or a reject Promise that triggers a reject, the then is skipped and goes to the next nearest catch. From this point of view, setting up multiple error handling in multiple steps is cumbersome, because you need to throw an error in a catch, or the promise that follows will be executed.

  1. Examples that can’t be skipped

Code:

constPrint and pass the value =(v) = > {
  console.log(v);
  return v;
};
new Promise((res, rej) = > {
  console.log('In process');
  rej('wrong');
})
  .catch((e) = > console.log(e)).then(print and pass values).catch((e) = > console.log(e)).then(print and pass values);Copy the code

Running results:

Error - undefined - undefined in processingCopy the code
  1. Example of jumping 1 then

Code:

constPrint and pass the value =(v) = > {
  console.log(v);
  return v;
};
new Promise((res, rej) = > {
  console.log('In process');
  rej('wrong');
})
  .catch((e) = > {
    console.log(e)
    throw new Error('throw wrong'}).then(print and pass values).catch((e) = > {
    console.log(e)}).then(print and pass values);Copy the code

Running results:

Error: Undefined is thrown incorrectlyCopy the code