# 리액트를다루는기술
# axios
# 동기적(synchronous) vs 비동기적(asynchronous)
# async/await
- https://triplexblog.kr/170 (opens new window)
- async/await : Promise를 더욱 쉽게 사용할 수 있도록 해 주는 ES2017(ES8) 문법. Promise가 끝날 때까지 기다리고, 결과 값을 특정 변수에 담을 수 있다.
- 함수의 앞부분에 async 키워드를 추가하고, 해당 함수 내부에서 Promise의 앞부분에 await 키워드를 사용
- axios : HTTP 요청을 Promise 기반으로 처리하는 자바스크립트 HTTP 클라이언트 라이브러리
function increment(number) {
const promise = new Promise((resolve, reject) => {
// resolve는 성공, reject는 실패
setTimeout(() => {
const result = number + 10;
if (result > 50) {
// 50보다 높으면 에러 발생시키기
const e = new Error("NumberTooBig");
return reject(e);
}
resolve(result); // number 값에 +10 후 성공 처리
}, 1000);
});
return promise;
}
async function runTasks() {
try {
// try/catch 구문을 사용하여 에러를 처리합니다.
let result = await increment(0);
console.log(result);
result = await increment(result);
console.log(result);
result = await increment(result);
console.log(result);
result = await increment(result);
console.log(result);
result = await increment(result);
console.log(result);
result = await increment(result);
console.log(result);
} catch (e) {
console.log(e);
}
}
runTasks();
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
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