1
doA( function1(){
    doC();

    doD( function2(){
        doF();
    } )

    doE();
} );

doB();

Assuming doA() and doD() to be asynchronous calls we have the sequence: A->B-C->D->E->F

  1. call A, returns and sends function1 to Queue,
  2. call B,
  3. Nothing more to execute -> Executes what's in the Queue -> call C,
  4. call D, returns and sends function2 to Queue,
  5. call E,
  6. 6-Nothing more to execute -> call F.

Is there something actually correct about my reasoning? Am I completely wrong?

This question came from reading "You don't know Javascript" by Kyle Simpson.

2 Answers 2

1

Seems like the book has done a good job, you've understood the basics well :-)

Some minor points may render your reasoning more precise:

  1. "Assuming doA() and doD() to be asynchronous calls" - let's say that doA and doB are ("asynchronous") functions that asynchronously call the callback functions passed to them as an argument. And we might further limit them to call the respective callback exactly once (to avoid confusion).

  2. "sends function1 to Queue" - it might not do so immediately. That's the whole point of background processing, the callback is only queued when the respective task (started by doA() or doD()) has finished. A simple example would be a timeout. If there are multiple asynchronous callbacks awaited, they might be called in arbitrary order, depending on how long their tasks did take.
    Similarly, when there is nothing more to execute, it might need to wait for something to appear in the queue, and idle as long as there is nothing.

  3. "what's in the Queue -> call C" - in fact, function1 is in the queue, which then calls doC() and the others. You shouldn't skip this step :-) Same for calling doF().

Sign up to request clarification or add additional context in comments.

Comments

0

You are almost there.

It is really hard to understand how the behavior will actually turn out. It can vary on how long and intensive each function call is in comparison to others running asynchronously.

Another part that you are missing is that "function1" will not inherently call on return of function "doA". For "function1" to even run, it is require for "doA" at sometime during its run to issues a callback(). Otherwise, "doA" will simply return and none of the nested functions you have written will be called.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.