1. 동기식 (Synchronous)
동기식은 요청을 보낸 후, 응답을 받아야지만 다음 동작이 이루어진다.
어떠한 일을 처리할 동안 다른 프로그램은 정지한다.
여러 개의 일을 진행해야 할 경우 (A, B, C, D, E)
A 시작 -> A 끝 -> B 시작 -> B 끝 -> C 시작 ->... E 끝
이러한 방식으로 일이 처리되기 때문에 시간이 오래걸리게 된다.
실제로 CPU가 느려지는 것은 아니지만, 시스템의 전체적인 효율이 저하된다고 할 수 있다.
2. 비동기식 (Asynchronous)
요청을 보낸 후, 응답과는 상관없이 다음 동작이 이루어진다.
결과가 주어지는데 시간이 걸리더라도 그 시간 동안 다른 작업을 할 수 있으므로 자원을 효율적으로 사용할 수 있다.
비동기식은 비동기식 처리를 요청할 때, 할 일이 끝난 후 처리 결과를 알려주는 콜백함수를 함께 알려준다.
- 비동기식 처리를 요청했을 때, 호출받은 함수는 바로 응답을 수행한다.
- 이 응답은 처리 결과에 대한 응답이 아니라, 요청에 대한 확인 동작일 뿐이다.
- 호출받은 함수는 처리가 끝나면 요청한 함수를 호출하여 처리 결과를 전달하게 된다.
따라서, 요청 후에 응답을 바로 받지 않고 다음 요청을 진행할 수 있기 때문에
http 전송 중에도 클라이언트가 웹 어플리케이션과 상호작용을 할 수 있는 것이다.