Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda