-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (48 loc) · 1.44 KB
/
index.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
// console.log(1);
// setTimeout(function () {
// console.log(2);
// }, 2000);
// console.log(3);
//////////////////////////////////
// ex. 편의점에 들어가서 음료수를 사고 나오는 상황
// function goMart() {
// console.log('마트에사거 어떤 음료를 살지 고민한다..');
// }
// function pickDrink() {
// //3초 기다린 후에 코드 실행 (= 3초 고민함)
// setTimeout(function () {
// console.log('고민 끝!!');
// product = '제로 콜라';
// price = 2000;
// }, 3000);
// }
// function pay(product, price) {
// console.log(`상품명: ${product}, 가격: ${price}`);
// }
// let product;
// let price;
// goMart();
// pickDrink();
// pay(product, price);
////////////////////////////////////////////////////////////////////
// 1. 콜백함수를 이용해 비동기 처리
function goMart() {
console.log('마트에사고 어떤 음료를 살지 고민한다..');
}
function pickDrink(callback) {
//callback은 다른 이름으로 작성해도 무방
// *callback 매개변수: 콜백함수를 의미
//3초 기다린 후에 코드 실행 (= 3초 고민함)
setTimeout(function () {
console.log('고민 끝!!');
product = '제로 콜라';
price = 2000;
callback(product, price); // *매개변수로 넘긴 콜백함수 실행
}, 3000);
}
let product;
let price;
goMart();
pickDrink(function pay(product, price) {
console.log(`상품명: ${product}, 가격: ${price}`);
});