fetch API

ES6를 정리할 때 더 자세하게 정리할 예정이니 간단하게 복습하기

fetch('html').then(funtion() {})

fetch() : 클라이언트가 서버에 인자에 전달된 파일을 요청한다.

then() : 응답이 끝나면 이 메서드를 실행한다.

function response() {
  console.log(success);
};

fetch('html').then(response);
console.log(one);
console.log(two);

위의 코드를 실행하면 console.log는 어떤 순서로 실행 될까?

// 결과
one
two
success

왜 response() 함수가 먼저 실행되지 않았을까?

ajax는 비동기(asynchronous) 통신이기 때문이다. 비동기 통신은 기다리지 않고 한 번에 여러가지 일을 수행할 수 있다.

반대로 동기(synchronous)는 여러가지 일을 수행할 수 없어서 기다리며 순서대로 진행된다.

Last updated

Was this helpful?