π Earlier than understanding what callback hell is, be sure to get conversant in the callback first.
In programming, callback hell refers to an ineffective manner of writing asynchronous code. It takes place while you nest capabilities and create too many ranges of nesting. This makes it tougher to regulate entry to the precise perform and it turns into tougher to debug the problems.
Nesting means putting one perform in one other, then putting the third perform within the second, then the fourth within the third one, and so forth. By putting callback within the callback and callback within the callback, with extra complicated functions callback logic may get too messy! This sort of nesting visually appears to be like like a pyramid which is why callback hell can be referred to as a pyramid of doom. πΉ
Letβs keep in mind what the callback perform is in motion whereas we put together some Italian Moka espresso.
Purchase some espresso earlier than filling the boiler
Fill the boiler earlier than filling the funnel
Fill the funnel earlier than putting the pot on range
Place the pot on the range earlier than eradicating it
Take away the pot earlier than pouring espresso
There you go, we made some espresso by utilizing a callback perform named nextAction in every of the capabilities. So every step will result in the following, then to the following and so forth – one actions is not going to begin till the earlier one is completed.
However what if I begin getting ready some salad the place I’ve to go although 10x extra steps? And what if I need to put together soup as effectively? And possibly some extra meals due to I’ve many friends?
Thatβs why I’ll wrap all actions into one perform and delegate this perform job to my husband – so every little thing can be completed by solely him.
And right here we obtained some callback hell the place finalStep is nested inside startPreparing which is nested as a substitute prepareFunnel which is nested inside prepareBoiler which is nested inside prepareMoka⦠And issues can get even worse generally!
As a newbie, you may not encounter a callback hell anytime quickly nevertheless don’t fret! When you do, so long as you recognize what it’s you can be partly ready. There are some methods to keep away from this which I’ll clarify later. For now, take pleasure in some espresso! βοΈ