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