DEV Community

Cover image for Code Smell 78 - Callback Hell
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 78 - Callback Hell

Processing an algorithm as a sequence of nested callbacks is not clever.

TL;DR: Don't process calls in a callback way. Write a sequence.

Problems

  • Readability

  • Hard to debug.

  • Complexity

Solutions

  1. Change callbacks to sequence calls.

  2. Extract repeated Code

  3. Refactor.

Sample Code

Wrong

var fs = require('fs');

var fileWithData = '/hello.world';  
fs.readFile(fileWithData, 'utf8',