-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.js
33 lines (26 loc) · 1.08 KB
/
callback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
// Synchronous Callback Functions
const greeting = (name) => {
alert(name);
};
// Asynchronous Callback Functions
setTimeout(() => {
console.log("Two seconds are up");
}, 2000);
// If we wanna call a function and inside that there is something which give result after some delay, then instead of returning the result, we have to pass another function to that function and get the result as an argument!
const add = (a, b) => {
setTimeout(() => {
return a + b;
}, 2000);
};
const ans = add(3, 5); // Ans will store undefined as first the call stack (main) will be executed first, then the asynchronous functions will be executed
console.log(ans);
// Correct Way -> we have to pass another function as an argument to get the result asynchronously
const addUsingCallback = (a, b, callback) => {
setTimeout(() => {
callback(a + b);
}, 2000);
};
addUsingCallback(3, 5, (result) => {
console.log(result);
});