> For the complete documentation index, see [llms.txt](https://april.gitbook.io/learning-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://april.gitbook.io/learning-js/chapter-14./14.2/14.2.4.md).

# 14.2.4 콜백 헬

### 콜백의 단점

* 한번에 여러가지를 기다려야 하는 경우 콜백을 관리하기가 어려움
* 중첩된 코드 블록은 에러 처리가 어려움
* **비동기적 코드가 늘어나면 늘어날수록 버그가 없고 관리하기 쉬운 코드를 작성하기가 매우 어려움**
* 이러한 이유로 프라미스가 등장함

### 콜백 헬 예시

```javascript
const fs = require('fs');

fs.readFile('a.txt', function(err, dataA) {
  if(err) console.error(err);

  fs.readFile('b.txt', function(err, dataB) {
    if(err) console.error(err);

    fs.readFile('c.txt', function(err, dataC) {
      if(err) console.error(err);

      setTimeout(function() {
        fs.writeFile('d.txt', dataA+dataB+dataC, function(err) {
          if(err) console.error(err);
        });
      }, 60*1000);
    });
  });
});
```
