background

It is found that error processing is done by try catch when async await is used in the current project in the team. The try catch thing itself is designed to handle unknown errors; And a lot of try catches turned us from promse chain hell into try catch hell

Summarize the two current ways of handling async await errors

First, promise.then. Catch

If an asynchronous operation following await fails, the Promise object returned by the async function is rejected

Async function f() {await new Promise(function (resolve, reject) {throw new Error(' Error '); }); } f().then(v => console.log(v)).catch(e => console.log(e)) // Error: an Error occurredCopy the code

or

async function f() {
  await somethingThatReturnsAPromise()
  .catch(function (err) {
    console.log(err);
  });
}
Copy the code

2. Try catch

Async function f() {try {await new Promise(function (resolve, reject) {throw new Error(' Error '); }); } catch(e) { } return await('hello world'); }Copy the code

The solution

Add a data interception mechanism to await promise returns

After transforming