1

I have this two errors from eslint:

error Promise returned in function argument where a void return was expected error Promise executor functions should not be async

They comes from this code:

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

The code works, but I really don´t know who to solve the eslint problem.

Thanks, In advance.

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Jota
  • 81
  • 7
  • Does this answer your question? [Is it an anti-pattern to use async/await inside of a new Promise() constructor?](https://stackoverflow.com/questions/43036229/is-it-an-anti-pattern-to-use-async-await-inside-of-a-new-promise-constructor) – BraveButter Sep 22 '20 at 12:18

1 Answers1

0

Try this:

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

I guess what's happening here is ESLint is expecting the callback function in your promises to return void but they're returning promises since they are async.

See the "Return Value" section on this page from MDN.

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Anuj Pancholi
  • 1,153
  • 8
  • 13