Synchronicity is something important to consider when using JavaScript. JavaScript is single-threaded which means only one thread, or action, is able to run at a time. One action has to finish before another action can be started. A lot of javascript actions, or event handlers like click(), take place quite fast so running synchronous actions one right after the other in synchronous fashion is typical, unless one has to communicate with a server. Synchronous functions are sometimes called blocking functions because they will block any other event handler from doing their job until the first action has been completed. Asynchronous functions that start now can be finished later and that allows other event handlers to be initiated while waiting for completion. This has the effect of freeing up the browser so that communication with another server won’t cause delay for the rest of the page.
A callback is a function that is passed as an argument to another function.
Callback hell is where I don’t want to be because it’s so confusing! Actually, this scenario occurs when a cascade of event handlers is in play. Troubleshooting can be tricky when several functions are nested waiting for a server response or other action. The output can be unexpected if certain callbacks finish at different times.
We can avoid callback hell, which is also known as the “Pyramid of Doom”, by using Javascript promises. A promise is an object that exists in one of three states, either pending, fulfilled or rejected. Promises are designed to let us deal with a function’s return of values or error messages by the included methods of then(), catch(), and finally().