明确忽略承诺是个坏主意吗?

Is it a bad idea to explicitly ignore promises?

我有一个 api 调用触发异步函数 doSomething。在这个函数中,我有另一个异步函数,它为用户创建一个通知,但我不想等它完成。所以我删除了 await 语句。该代码有效,但这是一个坏主意还是坏做法,因为我收到“缺少等待异步函数调用”警告。

    public async createNotification(createdStuff: CreatedStuff): Promise<void> {
        const userNotification = new Notifcation(..);
        await userNotification.save();
    }

    public async doSomething(): Promise<CreatedStuff> {
        const createdStuff = await this.createStuff();
        await createdStuff.save();

        // here I removed the await statement
        this.notificationService.createNotification(createdStuff);

        return createdStuff;
    }

如果 createLogPlayNotification(...) 拒绝,您将 运行 进入 unhandledRejection event which you probably don't want as it will crash your app depending on your NodeJS version (see this link 以获得更多信息。

这并不意味着你等待它,你可以简单地在返回的承诺上添加一个.catch-handler:

// ..
this.notificationService.createLogPlayNotification(loggedPLay)
                        .catch(err => {
                            // do something with err, e.g. log it
                         });
return createdStuff;