What is a callback function?

0 votes
What is a callback function?
Jun 12, 2019 in Others by shubham
• 6,910 points
206 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, 2019 by sunshine
• 1,260 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, 2019 in Others by Frankie
• 9,810 points
107 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, 2019 in Others by ArchanaNagur
• 2,340 points
133 views
0 votes
1 answer

What is the impact of covid-19 on a global economy?

Covid-19 that is co-corona,vi-virus,d-development,19-In the year 2019.The ...READ MORE

answered Mar 19 in Others by Niroj
• 67,620 points
338 views
0 votes
1 answer

What is a future data type in Flutter?

Hi@akhtar, Future is a type that ‘comes from the ...READ MORE

answered Aug 11 in Others by MD
• 66,740 points
83 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
326 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
340 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, 2019 in Others by Priyaj
• 57,700 points
129 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, 2019 in Others by Junaid
151 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, 2019 in Others by sunshine
• 1,260 points
166 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, 2019 in Others by sunshine
• 1,260 points
303 views