-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-callbacks.js
55 lines (47 loc) · 1.1 KB
/
4-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
/* const geocode = (address, callback) => {
return setTimeout(() => {
const data = {
latitude: 0,
longitude: 0
}
callback(address, data)
}, 2000)
}
function getData(address, data){
console.log(address, ':\n' , data);
}
geocode('Philadelpia', getData) */
/*******calllback */
/* add(1, 2, sum)
function add(a, b, sum) {
sum(a+b)
}
function sum(result) {
console.log(result)
} */
/*****closure */
/* function mul(a, b) {
return function mulCallback(c) {
console.log(a * b * c)
}
}
let x = mul(1, 2)
console.log(x)
x(5)
*/
/*****Callback again */
const doRocketLaunch = (weather, callbackResult) => {
setTimeout(function checkingWeather() {
if (weather == 'Good') callbackResult(undefined, 'Mission Success!')
else callbackResult('Abort Mission', undefined)
}, 2000)
console.log('Checking weather....')
}
const callbackResult = (error, result) => {
if (error) {
return console.log(error);
}
console.log(result);
}
doRocketLaunch('Good', callbackResult)
//doRocketLaunch('Bad', callbackResult)