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

230
Views
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 answers
Answer question

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 Report

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 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!