---
title: "NodeJS | Callback Function"
url: "https://magecomp.com/blog/nodejs-callback-function/"
date: "2024-11-13T17:39:18+00:00"
modified: "2024-11-13T17:39:18+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "NodeJS"
word_count: 333
reading_time: "2 min read"
summary: "In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and interacting with the database. Let’s learn about callback in NodeJS.
What is ..."
description: "Master asynchronous programming in NodeJS using callbacks. Learn more about callback function in NodeJS"
keywords: "NodeJS"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Introduction to Node.js Modules"
    url: "https://magecomp.com/blog/node-js-modules/"
  - title: "How to Install Node.js on Windows?"
    url: "https://magecomp.com/blog/install-node-js-on-windows/"
  - title: "How to Install Node.js on Mac?"
    url: "https://magecomp.com/blog/install-node-js-on-mac/"
---

# NodeJS | Callback Function

_Published: November 13, 2024_  
_Author: Bharat Desai_  

![NodeJS Callback Function](https://magecomp.com/blog/wp-content/uploads/2024/11/NodeJS-Callback-Function-1024x512.webp)

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and interacting with the database. Let’s learn about callback in NodeJS.
## What is Callback?
A callback function is a special type of function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. Callback allows NodeJs to execute other tasks while waiting for a particular operation to complete.
The consumer of a callback-based API writes a function that is passed into the API. The provider of the API (called the caller) takes the function and calls back (or executes) the function at some point inside the caller’s body. The caller is responsible for passing the right parameters into the callback function. The caller may also expect a particular return value from the callback function, which is used to instruct further behaviour of the caller.
## Types of callback function
There are two ways in which the callback may be called: synchronous and asynchronous. Synchronous callbacks are called immediately after the invocation of the outer function, with no intervening asynchronous tasks, while asynchronous callbacks are called at some point later, after an asynchronous operation has completed.
## Syntax & Example of callback:
```
function function_name(argument, function (callback_argument){
    // callback body
})
```

The setTimeout() function in Node.js is a typical example of callback. The following code calls the asynchronous setTimeout() method, which waits for 1000 milliseconds, but doesn’t block the thread. Instead, the subsequent Hello World message, followed by the timed message.
**Example:**

```
setTimeout(function () {
    console.log('This prints after 1000 ms');
}, 1000);
console.log("Hello World");
```

**Output:**

Hello World
This prints after 1000 ms
**Example:**

```
var myCallback = function(data) {
    console.log('got data: '+data);
};
var usingItNow = function(callback) {
    callback('get it?');
};
```

Finally use it with this next line:
```
usingItNow(myCallback);
```

## Conclusion:
Callback allow to execute operation asynchronously in NodeJS. Use it effectively to master asynchronous programming in NodeJS. For any doubts or queries, leave a comment without any hesitation.


---

_View the original post at: [https://magecomp.com/blog/nodejs-callback-function/](https://magecomp.com/blog/nodejs-callback-function/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-08 23:23:23 UTC_  
