微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重构 Promise 代码以在新的 Promise 构造函数中移除 async/await

如何解决重构 Promise 代码以在新的 Promise 构造函数中移除 async/await

我一直在阅读并看到在 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/

任何帮助将不胜感激。

谢谢。

解决方法

正如我在上面的代码中看到的,连接在 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())
}
,

正如@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();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?