Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Promis Async Await
  4. / Lessons
  5. / 2

Callbacks in JavaScript

  • A callback is a function passed as an argument to another function and is executed after the completion of a task.
function fetchData(callback) {
setTimeout(() => {
console.log('1. fetching data');
callback();
}, 5000);
}
function processData() {
console.log('2. processing data');
}
fetchData(processData);
/*
After 5 sec
===========
1. fetching data
2. processing data
*/