0

I understand that the asynchronous callback function will be pushed into the callback queue. For eg:

setTimeout(function() {
   console.log('Async');
}, 0);

In the above case, the callback function is pushed into the callback queue.

Will the synchronous callback function will also be pushed into the callback queue?

function a(b)
{
 console.log('First function')
}

a(function b()
{
console.log('Sync Callback')
});
will the function b will also be pushed into the callback queue?

11
  • No, a callback is just a function. Ps, it's not a callback queue, it's an event loop. Commented Jan 4, 2021 at 10:32
  • I don't think setTimeout is async latentflip.com/loupe/… Commented Jan 4, 2021 at 10:35
  • 2
    @Keith those are different things. There is a callback queue (although its a task queue). The event loop pops items off the queue and runs them. Commented Jan 4, 2021 at 10:35
  • 1
    @evolutionxbox. What makes you think that? Commented Jan 4, 2021 at 10:36
  • 1
    @Keith I can see that we're clearly on the same page and that neither of us are trying to mislead, we're simply using different levels of abstractions in our arguments. Fair enough. Commented Jan 4, 2021 at 11:01

1 Answer 1

1

will function b also be pushed onto the callback queue?

Short answer: NO.

Slightly longer answer: In your second snippet you are passing function b as an argument to function a, this happens synchronously. However you're never using function b inside function a, so while function a will evaluate, function b will neither be put on the queue, nor evaluated.

If you wanted b to be evaluated emediatly you would need to call it inside of a:

function a(arg_b) {
  arg_b();
  console.log('First function')
}

a(function b() {
  console.log('Sync Callback')
});

If you instead wanted b to be put on the queue and evaluated at a later point, you would need to create a task (or a microtask):

function a(arg_b) {
  setTimeout(arg_b, 0);
  console.log('First function')
}

a(function b() {
  console.log('Sync Callback')
});

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.