NodeJS:Eslint错误的诺言

问题描述

我有eslint这两个错误

错误Promise在函数参数中返回,其中无效返回为 预期的错误Promise执行程序功能不应异步

它们来自以下代码

      const promiseFeature = new Promise(async (resolve) => {
      let objectProfile = await this.userFeaturesRepository.findById(id);
      objectProfile = await this.userFeaturesRepository.getProfileObj(myUserFeatures);
      await this.userFeaturesRepository.updateById(id,objectProfile);
      resolve()
    })
    
      const promiseIAM = new Promise(async (resolve) => {
      let objectIAM = await this.userIAMRepository.findById(id);
      objectIAM = await this.userIAMRepository.getIAMObj(myUserFeatures);
      objectIAM.email = objectIAM.email.toLowerCase();
      await this.userIAMRepository.updateById(id,objectIAM);
      resolve()
      })

      await Promise.all([promiseFeature,promiseIAM]);

代码有效,但是我真的不知道该由谁来解决这个问题。

谢谢, 提前。

解决方法

尝试一下:

      const promiseFeature = new Promise((resolve) => {
      (async() => {
      let objectProfile = await this.userFeaturesRepository.findById(id);
      objectProfile = await this.userFeaturesRepository.getProfileObj(myUserFeatures);
      await this.userFeaturesRepository.updateById(id,objectProfile);
      resolve()
      })();
      
    })
    
      const promiseIAM = new Promise((resolve) => {
      (async() => {
      let objectIAM = await this.userIAMRepository.findById(id);
      objectIAM = await this.userIAMRepository.getIAMObj(myUserFeatures);
      objectIAM.email = objectIAM.email.toLowerCase();
      await this.userIAMRepository.updateById(id,objectIAM);
      resolve()
      })();
      
      })

      await Promise.all([promiseFeature,promiseIAM]);

我想这里发生的事情是eslint期望您的promise中的回调函数将返回void,但是由于它们是async,因此它们正在返回promise。

请参阅this page from MDN上的“返回值”部分。