-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path4_Promise_Async_Await.js
63 lines (54 loc) · 1.71 KB
/
4_Promise_Async_Await.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
63
// Before using Promise Example
function sayHello(name, byeCallback) {
setTimeout(() => {
console.log(name + "님 안녕하세요.");
byeCallback();
}, 2000);
}
sayHello("Mike", function() {
console.log("안녕히 가세요");
});
// Using Promise Example
function sayHelloPromise(name) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(name + "님 안녕하세요.");
resolve();
}, 2000);
});
}
sayHelloPromise("Frank").then(() => console.log("안녕히 가세요."));
// Using Promise with Params Example
function sayHelloResolveWithParams(name) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (name === "Mike") {
console.log(name + "님 안녕하세요.");
resolve(name);
} else {
console.log(name + "님은 출입할 수 없습니다.");
reject(name);
}
}, 2000);
});
}
sayHelloResolveWithParams("Frank")
.then(name => console.log(name + "님 안녕히가세요."))
.catch(name => console.log(name + "님 안녕히가세요."));
sayHelloResolveWithParams("Mike")
.then(name => console.log(name + "님 안녕히가세요."))
.catch(name => console.log(name + "님 안녕히가세요."));
// Async - Await Example
function sayHelloAsync(name) {
return new Promise(resolve => {
setTimeout(() => {
console.log(name + "님 안녕하세요.");
resolve(name);
}, 2000);
});
}
async function sayByeAsync(name) {
const result = await sayHelloAsync(name);
console.log(result + "님 안녕히가세요.");
}
sayByeAsync("Minsu");