What is a callback function?

0 votes
What is a callback function?
Jun 12 in Others by shubham
• 6,870 points
48 views

1 answer to this question.

0 votes

Callback function is a function which is calling other function  

For example: 

1. console.log('Start')

2. setTimeout(() => {console.log('3 sec Timer'), 3000)}

3. console.log('Stop')

As other programming languages here to the code start with main() function, then when it comes to the first line of the code, it would be executed, so the output would be: Start

Then when it comes to the second line, setTimeout is a callback function as it is a function which has to be executed first, on the completion of it, it would call the second function i.e. console.log('3 sec timer')

but here the o/p 3 sec timer will not come as Node.js does asynchronous programming, the callback function is stored in the place named as callback queue till the main() function gets completed

So it would move to line three now, so the output now would be: 

Start

Stop

Once the main function execution gets completed, the event loop in Node.js would say to execute the callback function which is stored in the callback Queue.

so the final output would be:

Start

Stop

3 Sec Timer

answered Jun 12 by sunshine
• 1,200 points

Related Questions In Others

0 votes
1 answer

What is a name function in JavaScript & how to define it?

A named function declares a name as ...READ MORE

answered Mar 6 in Others by Frankie
• 9,810 points
23 views
+1 vote
1 answer

What is Node.js? I want a clear understanding about it.

Node.js is a runtime environment which allows ...READ MORE

answered Jun 17 in Others by ArchanaNagur
• 2,190 points
23 views
0 votes
1 answer

What is the difference between hadoop and google analytics ?

I will try and answer this as ...READ MORE

answered Aug 21, 2018 in Others by Frankie
• 9,810 points
36 views
+5 votes
6 answers

what is artificial neural network?

Machine Learning is a field of computer ...READ MORE

answered Dec 16, 2018 in Others by Raghumishra
• 240 points
160 views
+1 vote
1 answer

What is the difference between Dark Web and Deep Web?

The content that which you cannot find ...READ MORE

answered Feb 6 in Others by Priyaj
• 56,160 points
57 views
+1 vote
2 answers

What is the different between xml and json?

These are the differences:   It is JavaScript Object ...READ MORE

answered Feb 7 in Others by Junaid
40 views
0 votes
1 answer

What is functional testing?

Functional testing a type of testing where ...READ MORE

answered Feb 18 in Others by Jobin
39 views
0 votes
1 answer

What sort of testing is involved in functional testing?

The following types of testing are involved ...READ MORE

answered Feb 18 in Others by Jobin
46 views
0 votes
1 answer

What is the difference between loadash and underscore

Loadash makes iterating arrays, strings, and objects easy in ...READ MORE

answered Jun 21 in Others by sunshine
• 1,200 points
41 views
+1 vote
1 answer

what is the difference between error and stderr in Node.js?

Error is an object created by Node.js to handle ...READ MORE

answered Jul 4 in Others by sunshine
• 1,200 points
32 views