<aside> 💡 In very simple words, Callbacks are functions that you can pass as an argument to another function or sub-routine in a program that will make your life easier and help you in writing less code
</aside>
function square(n) {
return n * n;
}
function cube(n) {
return n * n * n;
}
function sumOfSomething(a, b, callbackFunc) {
return callbackFunc(a) + callbackFunc(b);
}
const a = 3;
const b = 7;
console.log(sumOfSomething(a, b, square));
That’s why, asynchronous functions were born.
function onDone() {
console.log("hi there");
}
setTimeout(onDone, 5000);
console.log("after setTimeout");
--OUTPUT--
// after setTimeout
// hi there
<aside> 💡 How does this work? Why did we get an output like that?
Everything is mentioned in detail in this page → 1.5 Async, Await, and Promises
</aside>
Common asynchronous functions given by JS out of the box →
fs.readFileaxios.getsetTimeout, setInterval