Javascript Custom Callback Function

Liton

I have one concern about javascript callback function. What I understood is callback function should allow other statements to proceed if it takes time. So I have created one custom callback function to check but I'm not getting expected result. Am I doing wrong anything here?

function test(param1,param2,cb){
  if(typeof(cb) === 'function') return cb(param1,param2)
  else console.log('im not a func');
}

function calbackFunc(a,b){
    console.log('Hi i am '+a+' '+b);  
}
setTimeout(function timeout(){
        console.log('timeout')
},0);

test('callback','function',calbackFunc);

console.log('console');

Output

"Hi I am callback function"

"console"

"timeout"

As per the callback function, 'console' should come first. but it's not happening. Like setTimeout is working fine. Then why my custom callback function behaving like setTimeout.

ronapelbaum

You're getting confused between the Stack and the Queue.

The Event Loop

In javascript, synchronous calls are going into the stack while asynchronous calls are going into the Heap, and when done are back in the Queue. A function is moving from the Queue to the Stack only when it's empty.

enter image description here

In your example

phase 1: call setTimeout()

this puts the function in the heap, and since the timeout is set to 0, it right away moves to the _queue. does it means that it's executing right away? no, because that your current function (the "main") hasn't finished yet.

phase 2: call test()

this code is synchronous! when calling test we add a function to the stack. then we add the cb() call to the stack. that means that we need to finish all both before we can proceed to phase 3.

phase 3: console.log

nothing to explain here

post phase 3

the current "main stack is finished, so the function from the queue is now added ti the stack, logging 'timeout'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript: custom callBack function

From Dev

Proper syntax for callback on custom javascript function / jQuery

From Dev

Javascript custom function with a callback parameter to be executed in a event listener with different parameters?

From Dev

JavaScript Multiple Callback Function

From Dev

Javascript array with callback function

From Dev

Usage of Javascript callback function

From Dev

Javascript Function with Callback and Parameters

From Dev

javascript callback on function

From Dev

Javascript with callback function not working

From Dev

OOP with Javascript and callback function

From Dev

Javascript callback function not work

From Dev

Creating a callback on javascript function

From Dev

Javascript anonymous callback function

From Dev

javascript callback function selection

From Dev

Javascript Callback function malfunction

From Dev

Callback with arrow function in javascript

From Dev

Add .done() callback to custom function

From Dev

Woocommerce order by custom callback function

From Dev

Add .done() callback to custom function

From Dev

jQuery function custom assambled callback

From Dev

javascript callback function on a separate thread

From Dev

javascript callback function in chrome extension

From Dev

Javascript callback function for confirm bootbox

From Dev

callback function in javascript replace() is not called

From Dev

object callback function is undefined Javascript

From Dev

Javascript function callback dependant on timeout

From Dev

javascript callback function in chrome extension

From Dev

callback function argument in javascript / jQuery

From Dev

JavaScript: breaking out of a callback function

Related Related

HotTag

Archive