THpubs
THpubs

Reputation: 8172

'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 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?

Upvotes: 8

Views: 27781

Answers (2)

WM1
WM1

Reputation: 451

I received same warning/error

Resolved: My await was waiting for a non-async method

Just made the 'waited for' function async and it works.

try{ 
await this.waited_for_function() //ERROR ts(80007) 'await' has no effect on this type of expression
this.function_runs_after_waiting()
 } 
catch(error){console.log(`error:${error.message}`)

from:

waited_for_function(){ 
  console.log(`creates warning above`}

to:

async waited_for_function(){
  console.log(`good to go!`}

Upvotes: 2

THpubs
THpubs

Reputation: 8172

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.

Upvotes: 10

Related Questions