Skip to content

Latest commit

 

History

History
9 lines (5 loc) · 1.24 KB

File metadata and controls

9 lines (5 loc) · 1.24 KB

Start the journey from the simple callback pattern to promise

As we all know about callbacks and their restrictions, we should already know that if you want to access the result of an asynchronous function, you have to reach it inside of its callback. And of course this strategy ends to nested callbacks (aka callback hell).

The first step to avoid nesting callbacks is that we must find a way to access the async function’s result outside of its callback to be able to send the result around that we wouldn’t have to nest everything (the idea sounds a little foggy at this point, but stay with me).

Unfortunately, there is no magic way to do that and we must define something like a link in the ancestor scope of the callback and assign the resolved value of the async function to that link.

Note: I use CommonJS (i.e. Node.js module ecosystem) for modularize, but you can use any other types (e.g., ES6 Modules, RequireJS, AMD, UMD) or create your own, It doesn’t matter.