-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharrowFunctions.js
More file actions
110 lines (97 loc) · 3.5 KB
/
arrowFunctions.js
File metadata and controls
110 lines (97 loc) · 3.5 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Run this file by using "node arrowFunctions"
*
* Arrow / Anonymous function is a function that does't
* have a name, it usually used for callbacks
*
* Arrow function is actually a value, so we can fill
* it into a variable
*/
// Anonymous function structure
let anonymousFunction = function () {
return "this is an anonymous function"
}
console.log('anonymousFunction():', anonymousFunction()) // "this is an anonymous function"
// Arrow function structure
let arrowFunction = () => {
return "this is an arrow function"
}
console.log('arrowFunction():', arrowFunction()) // "this is an arrow function"
// Arrow function can also have multipe parameter
let mutipleParameters = (firstWord, secondWord) => {
return `${firstWord} ${secondWord}`
}
console.log("mutipleParameters()", mutipleParameters("My", "toy")); // "My toy"
// If an arrow function have one parameter, we can simplify it without using braces "()"
let convertToHashtag = word => {
return `#${word}`
}
console.log("convertToHashtag():", convertToHashtag('beach')); // "#beach"
// If an arrow function have one statement inside it, we can simplify it without using curly braces "{}"
let convertToDollar = amount => `$${amount}`
console.log("convertToDollar():", convertToDollar(500)); // "$500"
/**
* Callback is a way to return a value inside a function
*
* It usualy used for function that needs time to return
* a value, such as internet requests
*
* To understand it, we need to know about Javascript built
* in function called setTimeout()
*/
/**
* setTimeout() delays the execution of the arrow function
* by how much milliseconds that we put in second parameter
*/
setTimeout(() => {
console.log('this log will be executed after 3000ms (3 seconds)'); // 👈 this line will be executed after 3 secs
}, 3000);
/**
* Below function will not return anything because our code
* is synchronous, means it reads the next line without
* waiting anything, and as we know, sendTimeout() delay
* the return statement, and synchronous does not wait
*/
function getMessage() {
setTimeout(() => {
return 'this is a string from getMessage()'
}, 3000);
console.log("getMessage() has finished reading all of it's codes!");
}
console.log('getMessage():', getMessage()); // undefined
/**
* However, we can trick synchronous function by using
* the callback pattern to wait the setTimeout() delay
*/
function getMessageWithCallback(cb) {
setTimeout(() => {
cb('this is a string from getMessageWithCallback()')
/**
* 👆 in callback pattern, the callback parameter should be
* inserted with anfunction
*
* Because cb is a function
* we can use the cb parameter like a function, and then
* call it with parameter of this function result
*/
}, 3000);
console.log("getMessageWithCallback() has finished reading all of it's codes!");
}
/**
* Because we already use the getMessageWithCallback() parameter
* like a function, so we need to fill the first parameter with a function
* in order to make getMessageWithCallback() work properly
*/
getMessageWithCallback((result) => {
// the result parameter will be filled with the getMessageWithCallback() string
console.log('getMessageWithCallback():', result); // successfull!
})
/**
* A callback pattern is used in most of Javascript method
* one of which is array forEach() method
*/
// .forEach() method is used to loop arrays
var joinedNames = ["Budi", "Siti", "Halim"]
var botNames = joinedNames.forEach((name) => {
console.log('.forEach():', name);
})