-
JS에서 비동기적으로 코딩하기개발공부/JavaScript 2023. 12. 21. 09:43
오늘은 JS에서 비동기 처리하는 방식에 대해 한번 더 정리하고자 한다.
이전에 프로젝트 때문에 비동기 공부를 했었는데.. 내용이 많다..
그래서 면접 공부할 때 찾아보기가 힘들어서 핵심만 간단하게 정리하려고 한다.
비동기가 필요한 이유
자바스크립트는 싱글 스레드이기 때문에 한번에 하나의 작업만 수행 가능!
즉, 이전 것이 완료되지 않으면 다른 업무 수행 불가
(대기시간이 길어짐 = 작업 시간 길어짐 = 성능 문제 발생)
비동기는 이전 것이 완료되지 않아도 다른 업무 수행 가능!
단, 로직이 끝났을 때 원하는 동작 수행을 위해 콜백 함수를 사용하면서 콜백 지옥이 발생할 수 있음.
(가독성 떨어짐, 로직 변경 어려움 등 문제 발생)
https://hani-develop-history.tistory.com/42
이를 해결하기 위한 방법으로 Promise와 async & await 가 있음
Promise
.then을 사용하여 promise가 처리될 때까지 대기
https://hani-develop-history.tistory.com/43
async & await
async는 항상 promise를 반환
await는 promise가 처리될 때까지 대기
https://hani-develop-history.tistory.com/44
728x90'개발공부 > JavaScript' 카테고리의 다른 글
[자바스크립트] 자료형 변경하기 (0) 2024.02.05 [자바스크립트] 배열과 집합 ([], set()) (0) 2024.01.31 [JavaScript]리프레시 토큰(Refresh Token) (2) 2024.01.09 수학 함수 (abs, ceil, floor, round, sort) (2) 2024.01.04 빈 값 (null, undefined, undeclared, NaN) (1) 2023.12.27