What is a callback function?

0 votes
What is a callback function?
Jun 12, 2019 in Others by shubham
• 6,910 points
160 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
90 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,320 points
98 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
• 41,680 points
285 views
0 votes
1 answer

What is the best path to become a Project Manger?

Assuming you don't have sufficient experience, Here are ...READ MORE

answered Jul 22 in Others by Sirajul
• 55,300 points
21 views
0 votes
1 answer

What is the career path of a project manager?

Once you are monitoring someone, so make ...READ MORE

answered Jul 22 in Others by Gitika
• 32,970 points

edited Jul 22 by Gitika 23 views
0 votes
1 answer
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
244 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
133 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
212 views