Problems with structuring async code – callback hell

In the previous section, we introduced the callback pattern as a way to deal with asynchronous calls. The pattern offers a structured way of dealing with such calls, in that we can always know what to expect in its method signature; the error is the first parameter, the second parameter is the response, and so on. But the pattern does have its shortcomings. The shortcomings might not be obvious at first, because you might only call the code like this:

openFile('filename', (err, content) => {  console.log( content );  statement4;  statement5;})statement2;statement3

What we see here is how we invoke the method openFile(). Once that runs to completion, the callback is called and, inside of the ...

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.