Joox
Joox

Reputation: 1

Refactoring Promise code to remove async/await in new promise constructor

I had been reading and saw that it is an anti pattern to use async/await in the promise constructor.

I had the following code in my current project and I wish to refactoring it but not very sure how I should go about doing it.

   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);
        }
    });
}

I had tried the following but it had error at the finally method

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

i'm using jsstore for the database https://jsstore.net/tutorial/connection/

Any help will be appreciated.

Thank you.

Upvotes: 0

Views: 486

Answers (2)

Bergi
Bergi

Reputation: 664876

As @Andrei suggests, you could just drop the new Promise line and replace resolve/reject with return/throw. But I guess what you are actually looking for is to use try/finally - don't invoke the promise methods for this:

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

Upvotes: 0

Andrei
Andrei

Reputation: 12196

As I see in the upper piece of code the connection is terminated inside of try. It is possible to reuse that. full equeivalent of your code should look like:

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;
    }
}

or, if you like promises api more than try catch and termination should be in the finally block then it would be like

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())
}

Upvotes: 1

Related Questions