Javascript
-
[Javascript] 실행 컨텍스트Javascript 2023. 3. 17. 09:57
들어가기 전에.. 자바스크립트 프로그램을 요술 기계처럼 이리 누르고, 저리 눌러가면서 작동을 한다. 오늘 글에서는 이러한 현상의 뒷면에 있는 자바스크립트 내부 구조를 설명한다. 자바스크립트에서 가장 중요한 핵심 개념 중 하나라고 생각한다. 실행 가능한 코드 (Executable Code) 자바스크립트 엔진은 실행 가능한 코드를 만나면 코드를 평가하여 => 실행 컨텍스트(Executable Context)로 만든다. 실행 가능한 코드 전역 코드: 전역 객체 WIndow 아래에 정의된 함수 ==> 전역 실행 컨텍스트(Global Execution Context) 함수 코드: 함수 ==> 함수 실행 컨텍스트(Functional Execution Context) eval 코드: eval 함수 (단, eval 코..
-
[Javascript] Promise 와 async/awaitJavascript 2023. 3. 14. 20:53
비동기 처리란? 자바스크립트는 기본적으로 싱글 스레드 언어이기 때문에, 하나의 호출 스택(call stack)에서 처리하는 프로세스이다. 이런 상황에서 자바스크립트는 비동기 처리를 하기 위해서 큐(Queue)를 사용한다. 하나의 호출 스택에서 동기적 처리가 끝나면 큐에서 대기중인 비동기 함수들을 처리한다. setTime, addEventListener 메서드, ajax라 불리던 XMLHttpRequest 객체, fetch API 등이 대표적으로 비동기적으로 실행된다. 콜백 지옥 비동기 작업은 비동기 처리가 끝난 이후에 다음 처리를 실행하기 위한 용도로 사용하는데, 이런 흐름을 위해서 지속적으로 콜백 함수를 실행한다면 결과적으로 ‘콜백 지옥’에 빠지게 된다. // setTimeout 비동기 작업을 하는 s..