Pular para o conteúdo principal

callback

A callback is basically a function who is passed as an argument to another function and is executed after some operation has been completed

example:

function greet(name, callback) {
console.log(`hello, ${name}`)
callback()
}

greet("Grazi", () => {
console.log("The callback function was called")
})

//output:
// hello, Grazi
// The callback function was called

Also, callbacks is very usefull to handle asynchronous operations:

const request = require('request');

function getData(url, callback) {
request(url, (error, response, body) => {
if (error) {
callback(error);
} else {
callback(null, body);
}
});
}

getData('https://www.example.com', (error, data) => {
if (error) {
console.error(error);
} else {
console.log(data);
}
});