Is it feasible to handle async await errors in this way?

when using async await to further optimize asynchronous events, it also leaves us with some problems to deal with- error handling

most students seem to use try catch to deal with
in the async function, and then I wonder if I can make this kind of error easier and judge the success / failure of the request by the return value

.
// api.js
export const getRecordList = (params) => fetch(URL, params, "POST")
      // res { code: 200, data: { list: [...], total: 999}, msg: ""}
      // returntrue
      .then(res => res.data || {})
      // return false
      .catch((err) => alert(err));

// RecordList.js
...
async getRecordList() {
    const { isLoading, searchParams } = this.data;
    if (isLoading) return;
    // --  --
    this.data.isLoading = true;
    const res = await API.getRecordList(searchParams);
    // res
    if (res) {
      // record
      const { list, total } = res;
      this.data.list = list;
      this.data.totalPage = Math.ceil(total / PAGE_SIZE) || 1;
    } else {
      // ...
    }
    this.data.isLoading = false;
    // --  --
  },
...

I think the above approach can also replace the try catch method, and it is clearer. I don"t know if it will work. Ask the bosses for advice

Sep.16,2021

!(async function(){
  function test(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            Math.random() > 0.5 ? resolve('normal') : reject('error')
        }, 1000)  
    })  
  }
  
  function to(promise){
    return new Promise((resolve, reject) => {
        promise.then(re => resolve([null, re])).catch(err => resolve([err, null]))
    })
  }
  [err, re] = await to(test())
  console.log(err, re)
})()

just return two parameters


there is a difference between logical handling and code exception catching of try catch .
what you do is if else conditional logic

Promise + try catch

 return new Promise((resolve, reject) => {
    try{
        //
        // 
        //if (res) {
        //resolve(res)
        //}else{
        //reject(res)
        //}
        resolve(res)
    }catch(e){
        reject(e)
    }
})

call the method with .then () .catch ()

Menu