小编Tch*_*loh的帖子

什么时候应该使用 try catch 而不是 then catch?

问题很简单,但我没有在任何地方找到答案。

我什么时候应该使用try catch?在下面的代码中,我使用 try catch 来处理请求的返回:

async findUsers() {
    this.loading = true;

    try {
        const [users, count] = await this.api.get('/users/list');

        this.users = users;
        this.totalPages = Math.ceil(parseInt(count) / 10);
    }
    catch (error) {
        this.Messages.requestFailed(error);
    }
    finally {
        this.loading = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 then(...).catch(...)是一个好习惯吗?

javascript asynchronous promise

20
推荐指数
1
解决办法
2万
查看次数

标签 统计

asynchronous ×1

javascript ×1

promise ×1