Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 579
THpubs Avatar asked Nov 30 '19 09:11

THpubs


People also ask

Does await in a for loop block?

No, await won't block the looping.

Can I use await inside for?

for await...of can only be used in contexts where await can be used, which includes inside an async function body and in a module. Even when the iterable is sync, the loop still awaits the return value for every iteration, leading to slower execution due to repeated promisepromiseIn other cases a future and a promise are created together and associated with each other: the future is the value, the promise is the function that sets the value – essentially the return value (future) of an asynchronous function (promise).https://en.wikipedia.org › wiki › Futures_and_promisesFutures and promises - Wikipedia unwrapping.

Does await only works inside an async function?

Because await is only valid inside async functions and modules, which themselves are asynchronous and return promisespromisesIn other cases a future and a promise are created together and associated with each other: the future is the value, the promise is the function that sets the value – essentially the return value (future) of an asynchronous function (promise).https://en.wikipedia.org › wiki › Futures_and_promisesFutures and promises - Wikipedia, the await expression never blocks the main thread and only defers execution of code that actually depends on the result, i.e. anything after the await expression.

Can we use await inside callback?

We always use the AWAIT keyword inside an async function and the reason for that is, we need to make sure that the promisespromisesIn other cases a future and a promise are created together and associated with each other: the future is the value, the promise is the function that sets the value – essentially the return value (future) of an asynchronous function (promise).https://en.wikipedia.org › wiki › Futures_and_promisesFutures and promises - Wikipedia returned in the async function are synchronized. Await eliminates the use of callbacks in .


2 Answers

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.

like image 52
THpubs Avatar answered Nov 16 '22 01:11

THpubs


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!`}
like image 25
WM1 Avatar answered Nov 16 '22 00:11

WM1