Promises versus callbacks

Suppose you wanted to perform three AJAX requests one after another. Here's a dummy implementation of that in callback-style:

ajaxCall('http://example.com/page1', response1 => {   ajaxCall('http://example.com/page2'+response1, response2 => {     ajaxCall('http://example.com/page3'+response2, response3 => {       console.log(response3)     }   })})

You can see how quickly you can enter into something known as callback-hell. Multiple nesting makes code not only unreadable but also difficult to maintain. Furthermore, if you start processing data after every call, and the next call is based on a previous call's response data, the complexity of the code will be unmatchable.

Callback-hell refers to multiple asynchronous functions nested ...

Get Learn ECMAScript - Second Edition 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.