-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.js
62 lines (49 loc) · 2.15 KB
/
callbacks.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Cem Eygi. freeCodeCamp.org. (2020). JavaScript Promise Tutorial: Resolve, Reject, and Chaining in JS and ES6. [online]
Available at: https://www.freecodecamp.org/news/javascript-es6-promises-for-beginners-resolve-reject-and-chaining-explained/
[Accessed 9 Mar. 2022].
Cem Eygi.freeCodeCamp.org. (2020). JavaScript Callback Functions – What are Callbacks in JS and How to Use Them. [online]
Available at: https://www.freecodecamp.org/news/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them/
[Accessed 9 Mar. 2022].
*/
//The print() function takes another function as a parameter and calls it inside.
//This is valid in JavaScript and we call it a “callback”.
//So a function that is passed to another function as a parameter is a callback function.
function print(callback) {
callback();
}
//the message function is an example of a callback function.
//it is being passed into setTimeout()
const message = function() {
console.log("This message is shown after 4 seconds");
}
setTimeout(message, 4000);
//Anonymous Function
//a function definition without a name in JavaScript is called as an “anonymous function”.
//the callback function here has no name
setTimeout(function() {
console.log("This message is shown after 3 seconds");
}, 3000);
//arrow function
setTimeout(() => {
console.log("This message is shown after 3 seconds");
}, 3000);
//callback hell (hard to read) (methods are not defined so youll get error on compiling)
firstRequest(function(response) {
secondRequest(response, function(nextResponse) {
thirdRequest(nextResponse, function(finalResponse) {
console.log('Final response: ' + finalResponse);
}, failureCallback);
}, failureCallback);
}, failureCallback);
//using chaining, a feture of promises. (easier to read) (methods are not defined so youll get error on compiling)
firstRequest()
.then(function(response) {
return secondRequest(response);
}).then(function(nextResponse) {
return thirdRequest(nextResponse);
}).then(function(finalResponse) {
console.log('Final response: ' + finalResponse);
}).catch(failureCallback);