Categories
discuss

‘await’ has no effect on the type of this expression when using await inside an if block

In my code I have an array of user names. I’m trying to go through each name, check whether the user exist in the database and create the user. The problem is, my linter is saying ‘await’ has no …

In my code I have an array of user names. I’m trying to go through each name, check whether the user exist in the database and create the user. The problem is, my linter is saying 'await' has no effect on the type of this expression in the places I have marked:

await Promise.all(
  userNames.map(async userName => {
    const user = await users.findOne({ name: userNames });

    if (!user)
      await users.create({ // Linter marks this bracket (Not the curly bracket)
        name: userName,
      }); // And this end bracket
  }),
);

My editor suggests something like this:

  if (user) {
    return;
  }
  await users.create({
    name: userName,
  });

Flipping the if else. Any idea why?

Answer

The issue was, users.create is not a promise to be awaited! One good way to find this is to click on ctrl + click on the method and check it’s type definitions.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..