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

165
Vistas
Confusion on setTimeout() and asynchronicity

I have a bit of confusion about setTimeout(callback, delay) and the general definition of asynchronicity in Javascript. These are my two questions...

  1. How is it that setTimeout()'s delay timer executes ("counts down") immediately even while the main thread is still running (has not completed the call stack)? Is it using web workers or a separate thread behind the scenes?

  2. When programmers talk of "Asynchronicity" in Javascript, are they talking about the ability to execute some code in parallel with the main thread (like how the delay portion of setTimeout works), OR are they talking about the ability to simply postpone a function from executing until the main thread's function stack has emptied (ie simply pushing a callback onto the event queue for later execution like how the callback portion of setTimeout works)?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

How is it that setTimeout()'s delay timer executes ("counts down") immediately even while the main thread is still running (has not completed the call stack)?

There's no code sitting there decrementing a counter. The implementation is allowed to vary between environments (since setTimeout is provided by the environment, it's not part of JavaScript), but here's a likely one: Since the main thread services a job queue, an implementation could look at the list of timers prior to pulling the next job from the queue and, if it's time for a timer to finish, add a job to the queue to call its callback. Then it processes that call in order from the queue when it comes up.

When programmers talk of "Asynchronicity" in Javascript, are they talking about the ability to execute some code in parallel with the main thread (like how the delay portion of setTimeout works), OR are they talking about the ability to simply postpone a function from executing until the main thread's function stack has emptied

Probably both and more, but also less. The point of flagging up that something is asynchronous is to say "you won't see any result for this until the current task completes" (since no asynchronous callback can occur until the task completes). Why is usually clear from context: Because we have to wait for a network resource to arrive; because we have to wait for the user to click something; because we have to wait until the browser is about to render the next frame; because we have to wait for a timer to fire; because... You get the idea. :-)

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