Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda