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

231
Vistas
What if we do not wait for an asynchronous javascript function?

What if we do not wait for an asynchronous javascript function?

As far as I know some languages like C # should not run an asynchronous function unmanaged!

I wanted to know if this is also true for the JavaScript language?

var asynchronousFunction = async function() {
    //...
}

function main() {
   var result = true;
   //...
   asynchronousFunction(); // The result of this function has no effect on our output (result)
   //...
   return result;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It's run just the same. (In fact, you never await a function, you await for the the Promise it returns.)

The asynchronous function is run synchronously until the first await or return within it, at which point a Promise is returned to the caller and the rest of the function is arranged to run later.

It's up to the caller to do something (or nothing) to the Promise. After all, you might wish to store the promise in an array and await for the lot of them (Promise.all) or do something more esoteric about it, so JavaScript itself doesn't care.

Some smart enough IDEs and linters are able to raise a warning about unhandled promises, though, especially if you have enough type information to do so (e.g. by using TypeScript).

about 4 years ago · Juan Pablo Isaza Denunciar

0

It's true for javascript as well.

You don't want to just create a promise and leave it totally hanging, if there are errors then they become unhandled errors and if it exits unexpectedly then you have no way of knowing that.

What I recommend is using the Promise.race at the top level and then it will run all of your async functions in parallel but will exit if any one of them exits unexpectedly.

async function worker() {
  while (true) {
    // do background work in a loop
  }
}

async function server() {
  await init()
  await listen()
}

function main() {
  const p0 = worker()
  const p1 = server()

  try {
    await Promise.race([p0, p1])
    console.log('done')
    return true
  } catch (err) {
    console.log('The server had an error unexpectedly', err)
    return false
  }
}

If you expect the promises to eventually exit gracefully then use Promise.all instead, which will wait until all promises exit successfully before resolving.

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