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

210
Views
Node resolving not-awaited promises in main script

Why is "timeout' printed by node before terminating when I execute the following code ?

const shouldNotPrint = async () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('timeout')
      resolve('foo')
    }, 300)
  })
}

shouldNotPrint()
console.log('ended')

Running the previous code with node 16.13.0 will output

> node src/index.js
ended
timeout

Is there some kind of top-level await in node waiting for all promises to resolve before ending the process ?

To clarify: I do not want to terminate Node before the end of my promise. I just want to understand, at a low level, the behavior of node and the event-loop in that case. I understand the logic of a timeout + promise and why it would be printed after "ended". The question is why it is displayed at all and why Node is waiting for it to be displayed before terminating the process.

about 4 years ago · Juan Pablo Isaza
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!