부트캠프/TIL

코드스테이츠 프론트엔드 부트캠프 Day 33. 구조 분해 할당, fetch

하이고니 2023. 2. 2. 17:25

함수의 인자가

 

const search = ({ departure, destination }) => {}

 

이런 형태라면,

 

{departure: value1, destination: value2}

 

'구조 분해 할당해서 value1과 value2가 입력되는 형태구나'

바로 이해해야 한다.

 

 

 

fetch

fetch는 Promise 객체를 리턴한다.

 

fetch('https://koreanjson.com/posts/1');	// Promise 리턴

 

Promise 안에는 PromiseState와 PromiseResult가 있다.

PromiseResult 안에 우리가 원하는 데이터가 있다. 어떻게 꺼내쓰면 될까?

 

fetch('https://koreanjson.com/posts/1')
	.then((res) => console.log(res))	// Response { ~~~ }

 

then도 Promise 객체를 리턴한다.

console만 찍었으므로 지금 PromiseResult는 undefined

 

fetch('https://koreanjson.com/posts/1')
	.then((res) => res)

 

이렇게 해주면, PromiseResult는 Response

 

await fetch('https://koreanjson.com/posts/1');

 

await를 써주면 바로 Response 반환

await가 리턴하는 것은 해당 프로미스 객체의 PromiseResult 값