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

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

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