- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.js
More file actions
Latest commit
70 lines (62 loc) · 1.71 KB
/
Copy pathcallback.js
File metadata and controls
70 lines (62 loc) · 1.71 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
'use strict';
// Synchronous callback
functionprintImmediately(print){
print();
}
// Asynchrounous callback
functionprintWithDelay(print,timeout){
setTimeout(print,timeout);
}
// JavaScript is synchronous
// Execute the code block by orger after hoisting
// hoisting: var, function declaration
console.log('1');// 동기
setTimeout(()=>console.log('2'),1000);// 비동기
console.log('3');// 동기
printImmediately(()=>console.log('hello'));// 동기
printWithDelay(()=>console.log('Asynchrounous callback'),2000);// 비동기
// Callback Hell example
classUserStorage{
loginUser(id,password,onSuccess,onError){
setTimeout(()=>{
if(
(id==='sill'&&password==='dream')||
(id==='coder'&&password==='academy')
){
onSuccess(id);
}else{
onError(newError('not found'));
}
},2000);
}
getRoles(user,onSuccess,onError){
setTimeout(()=>{
if(user==='sill'){
onSuccess({name: 'sill',role: 'admin'});
}else{
onError(newError('no access'));
}
},1000);
}
}
constuserStorage=newUserStorage();
constid=prompt('enter your id');
constpassword=prompt('enter your password');
userStorage.loginUser(
id,
password,
user=>{
userStorage.getRoles(
user,
userWithRole=>{
alert(`Hello ${userWithRole.name}, you have a ${userWithRole.role} role`)
},
error=>{
console.log(error);
}
);
},
error=>{
console.log(error);
}
);