์›น/JavaScript

[Javascript] ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ, fetch, await, async

Judith Hopps 2023. 1. 14. 15:15
๋ฐ˜์‘ํ˜•

๐ŸŒ ์˜ˆ์ œ์šฉ ์„œ๋ฒ„ ์š”์ฒญ

๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ

https://showcases.yalco.kr/javascript/mockserver/race-result

๊ฐ ์„ ์ˆ˜๋“ค ์ •๋ณด

https://showcases.yalco.kr/javascript/mockserver/runners/{1~5}

ํ•™๊ต ์ •๋ณด

https://showcases.yalco.kr/javascript/mockserver/schools/{1~3}




 

Fetch API

  • Web API์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ - ์ฆ‰ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณต
  • ๋„คํŠธ์›Œํฌ๋กœ๋ถ€ํ„ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•˜๊ณ  ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ๋“ค ์ œ๊ณต
  • ๐Ÿ‘‰ MDN ๋ฌธ์„œ ๋ณด๊ธฐ
  • ๋ณด๋‹ค ์˜ค๋ž˜๋œ ๋ฐฉ๋ฒ•: ๐Ÿ”— XMLHttpRequest



fetch ๋ฉ”์„œ๋“œ

  • ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์œผ๋กœ ์›๊ฒฉ์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๋‹ต์„ ๋ฐ›์•„์˜ค๋Š” ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜
  • ๐Ÿ‘‰ MDN ๋ฌธ์„œ ๋ณด๊ธฐ
// ๐Ÿ’ก ๊ฒฐ๊ณผ๊ฐ€ Promise์˜ ์ธ์Šคํ„ด์Šค์ž„ ํ™•์ธ
console.log(
);

 

.then(response => {
console.log(response);
return response;
})
.then(response => response.json())
.then(console.log);

๋ฐ˜ํ™˜๋˜๋Š” ๊ฒฐ๊ณผ ( response )

  • ์š”์ฒญ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์„ ๋‹ด์€ ๊ฐ์ฒด
  • json ๋ฉ”์„œ๋“œ - ๊ฒฐ๊ณผ์˜ body๋กœ ๋ฐ›์€ ํ…์ŠคํŠธ๋ฅผ JSON ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜



์ฃผ์†Œ ๋“ฑ์ด ์ž˜๋ชป๋œ ๊ฒฝ์šฐ ๋“ฑ ์—๋Ÿฌ์ƒํ™ฉ์‹œ catch์—์„œ ์ฒ˜๋ฆฌ

fetch('https://WRONG-ADDRESS')
.then(response => response.json())
.then(console.log)
.catch(msg => {
console.error(`๐Ÿ˜ณ ์—๋Ÿฌ ๋ฐœ์ƒ: ${msg}`)
})
.finally(() => {
console.log('- - ํ†ต์‹  ์ข…๋ฃŒ - -')
})




 

์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ๋“ค ๋ฏธ๋ฆฌ๋ณด๊ธฐ

 

fetch(SERVER_URL + 'race-result')
.then(response => response.json())
.then(console.log)
.catch(console.error);

 

[1, 2, 3, 4, 5].forEach(item => {
fetch(`${SERVER_URL}runners/${item}`)
.then(response => response.json())
.then(console.log)
.catch(console.error);
});

 

[1, 2, 3].forEach(item => {
fetch(`${SERVER_URL}schools/${item}`)
.then(response => response.json())
.then(console.log)
.catch(console.error);
});




 

๐Ÿ”— ์—ฐ์† fetching ์˜ˆ์ œ

  1. ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜จ ๋’ค 1๋“ฑ ์ฃผ์ž ์„ ํƒ
  2. ํ•ด๋‹น ์ฃผ์ž์˜ ์ƒ์„ธ์ •๋ณด ๋ฐ›์•„์˜จ ๋’ค ํ•™๊ต ์ฝ”๋“œ ์ถ”์ถœ
  3. ํ•ด๋‹น ํ•™๊ต์˜ ์ •๋ณด ๋ฐ›์•„์˜ค๊ธฐ



1. ํ”„๋กœ๋ฏธ์Šค ํ˜•ํƒœ๋กœ ๊ตฌํ˜„

โ€‹
fetch(SERVER_URL + 'race-result')
.then(result => result.json())
.then(arry => {
return arry.sort((a, b) => {
return a.record - b.record
})[0].runner_idx
})
.then(winnerIdx => {
return fetch(`${SERVER_URL}runners/${winnerIdx}`)
})
.then(result => result.json())
.then(({school_idx}) => school_idx)
.then(schoolIdx => {
return fetch(`${SERVER_URL}schools/${schoolIdx}`)
})
.then(result => result.json())
.then(console.log)
.catch(console.error);



2. async, await์œผ๋กœ ๊ตฌํ˜„

โ€‹
async function getWinnersSchool () {
โ€‹
const raceResult = await fetch(SERVER_URL + 'race-result')
.then(result => result.json());
โ€‹
const winnerIdx = raceResult
.sort((a, b) => {
return a.record - b.record
})[0].runner_idx;
โ€‹
const winnerInfo = await fetch(`${SERVER_URL}runners/${winnerIdx}`)
.then(result => result.json());
โ€‹
const schoolIdx = winnerInfo.school_idx;
โ€‹
const schoolInfo = await fetch(`${SERVER_URL}schools/${schoolIdx}`)
.then(result => result.json());
โ€‹
console.log(schoolInfo);
}
โ€‹
getWinnersSchool();
 

 

๋ฐ˜์‘ํ˜•