【问题标题】:Refactoring Promise code to remove async/await in new promise constructor重构 Promise 代码以在新的 Promise 构造函数中删除 async/await
【发布时间】:2021-01-21 11:03:02
【问题描述】:

我一直在阅读并看到在 Promise 构造函数中使用 async/await 是一种反模式。

我当前的项目中有以下代码,我希望重构它,但不太确定我应该如何去做。

   updateFileContentDownloaded(dataId: string[]): Promise<number> {
    return new Promise(async (resolve, reject) => {
        await this.db.initConnection2();
        await this.db.connection2.openDb(dbName);
        try {
            const rows = await this.fileContentRepository.updateFileContent(dataId, this.db.connection2);
            await this.db.connection2.terminate();
            resolve(rows);
        } catch (err) {
            console.log('An error occur at get updateFileContentDownloaded method');
            reject(err);
        }
    });
}

我尝试了以下方法,但在 finally 方法中出现错误

    async updateFileContentDownloaded(dataId: string[]): Promise<number> {
    await this.db.initConnection2();
    await this.db.connection2.openDb(dbName);
    return await this.fileContentRepository.updateFileContent(dataId, this.db.connection2).then((rows) =>
        Promise.resolve(rows)
    ).catch(() =>
        Promise.reject(0)
    ).finally(() => await this.db.connection2.terminate());
}

我正在为数据库使用 jsstore https://jsstore.net/tutorial/connection/

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 在 finally 调用中删除 await 关键字。也只是 return await this.fileContentRepository.updateFileContent(dataId, this.db.connection2).finnally... 没有 thencatch 就足够了,因为你在处理程序内部没有做任何有用的事情

标签: angular promise anti-patterns


【解决方案1】:

正如我在上面的代码中看到的,连接在 try 内终止。可以重复使用它。您的代码的完整等效项应如下所示:

async updateFileContentDownloaded(dataId: string[]): Promise<number> {
    await this.db.initConnection2();
    await this.db.connection2.openDb(dbName);
    try {
      const rows = await this.fileContentRepository.updateFileContent(dataId, this.db.connection2);
      await this.db.connection2.terminate();
      return rows;
    } catch (err) {
      console.log('An error occur at get updateFileContentDownloaded method');
      throw err;
    }
}

或者,如果你更喜欢 promises api 而不是 try catch 和终止应该在 finally 块中,那么它会像

async updateFileContentDownloaded(dataId: string[]): Promise<number> {
    await this.db.initConnection2();
    await this.db.connection2.openDb(dbName);
    return this.fileContentRepository.updateFileContent(dataId, this.db.connection2)
      .finally(() => this.db.connection2.terminate())
}

【讨论】:

    【解决方案2】:

    正如@Andrei 所建议的,您可以删除new Promise 行并将resolve/reject 替换为return/throw。但我猜你真正想要的是使用try/finally - 不要为此调用promise方法:

    updateFileContentDownloaded(dataId: string[]): Promise<number> {
        await this.db.initConnection2();
        await this.db.connection2.openDb(dbName);
        try {
            return await this.fileContentRepository.updateFileContent(dataId, this.db.connection2);
        } catch (err) {
            console.log('An error occur at get updateFileContentDownloaded method');
            throw err;
        } finally {
            await this.db.connection2.terminate();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 1970-01-01
      • 2020-09-22
      • 2020-02-05
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2015-07-14
      相关资源
      最近更新 更多