Reputation: 1943
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')
});
Upvotes: 0
Views: 297
Reputation: 6872
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')
});
Upvotes: 1