# 재귀, 비동기와 호출 스택

비동기함수를 이용하면 호출스택이 터지지 않고 함수를 호출할 수 있다. 그 원리는 아래와 같다.

```javascript
function a() {
    setTimeout(function() {
        a();
    },0);
}
a();
```

호출 스택에 a,setTimeout 순으로 담기고 setTimeout이 호출되면 setTimeout의 함수가 실행되지 않고 이 부분을 어딘가에 저장하고 호출 스택에서 나간다. 이렇게 setTimeout이 나가면 a도 호출 스택에서 나간다.

(호출 스택이 비면 어딘가에 저장했던 setTimeout의 익명함수가 0ms 후에 실행된다. 이 때 호출 스택에 익명함수, a, setTimeout이 순서대로 담기고, 역순으로 사라진다. 호출 스택이 비면 어딘가에 저장했던... ) => 이런 식으로 호출 스택이 터지지 않고 차고 비는 것이 반복된다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/zerocho-javascript/undefined-24.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
