Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Async call waiting for variable to be settled in Javascript

My question is very simple. I have a global variable, someVar, that is initially set to null. Then I have a function like below:

someFunc: async function () {
   someVar = await someAsyncHelperThatReturnsABoolean();
   var anotherVar = someVar;
   // call some function based on the state of anotherVar
}

I want to ensure that the anotherVar is not going to be set to null due to the fact that someVar was initially null. In other words, I want to make sure that anotherVar is set to a proper boolean that is the response type of someAsyncHelperThatReturnsABoolean().

Based on my understanding, using the await keyword means that any other part of the code within someFunc that uses someVar will wait (the keyword is await after all) for the line someVar = await someAsyncHelperThatReturnsABoolean(); to properly finish and for someVar to be actually set to a proper boolean. Is that correct?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yes and no.

The await keyword inside an async function will wait for the given Promise to fulfill, unless it isn't a Promise, in which case the value is directly returned. If the Promise got rejected, the await keyword throws the rejected value. If the Promise got resolved, it returns the resolved value. If that's a Promise, it'll recursively await it. Mind that (besides recursively awaiting Promises) it doesn't actually do/check anything with the value. If your someAsyncHelperThatReturnsABoolean eventually resolves to null, you'll still end up with null.

On a side note: unless you already declared and initialized someVar before, it should be undefined, not null. Also make sure to declare someVar within your function. If it's a global, you might run in other issues, although unlikely. But good coding practices are good to follow anyway.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!