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

255
Vistas
Understanding JS callbacks and asynchronous operations

I'd like to walk through my thought process on pseudocode used in You Dont Know JS to demonstrate sequence of flow of asynchronicity.

doA( function1(){
     doC();
    doD( function2(){
    doF();
    } )
    doE();
    } );
doB();

He assumes doA and doD are async in his conclusion.

  1. A -> B -> C -> D -> E -> F is the order of operation according to the book. In my understanding, doB() runs second because it's next on the call stack since doA is async and does not block the thread. Function 1 is pushed to queue.

  2. Call stack is empty; function 1 is called from the queue. doC() is executed.

  3. Call doD and push function2 to the stack, doE() can then be called. Call stack empty, function 2 is called and doF() is called.

Secondly, he also states that, if A and D were synchronous, the thread will move like A,C,D,F,E,B. How would that work? Is it because doA() hogs the entire thread so doB() has no choice but to wait?

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

0

The asynchronous callback is said to be when its execution record is added to the callback queue. Then it will act as an asynchronous callback.

Remember any function is executed when it comes inside the call stack.

At first, you stated that he assumed that A & D are async so the order of operation is correct because functions declared inside A & D are added to the queue. But this is also hypothetical as functions inside js work differently.

Secondly, he assumed that they are sync. These type of callbacks is generally called synchronous callbacks.

Not every function passed to Javascript functions act as a callback function. This type of passed function is similar to how we pass an object to a function. They depend upon how we called them in function definitions. I don't know the function definitions that you described.

As in the book, order of operation A, C, D, F, E, B is correct. And yes doA halts all other executions as A & D contains all synchronous functions.

See this http://www.jsv9000.app/ JS visualizer and see what happen when you pass the callback function to the general user-created function and setTimeout or addEventListner.

When you use setTimeout or addEventListner, javascript implicitly push function records to the queue as the queue is a core part of js. If you want to use asynchronicity for user-created functions then you may have to explicitly push it to callback queue or the best option is by using promises. I hope you will understand my points.

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