1. What is the difference between synchronous and asynchronous functions?
Synchronous code is executed in sequence, where each function block is dependent on the previous block finishing before it can be executed. Asynchronous code doesnât need to wait as the functions are set aside and executed at a later stage so the main program can keep running, meaning a webpage or app will remain responsive and prevent the end user having to waste time waiting on each piece of code to complete.
2. What is callback hell?
Itâs a conventional name given to poor coding practice. A callback is a function thatâs passed to another function as an argument to be executed later, and when this repeatedly happens and say the second function then gets passed to a third function, and so on, callback hell can develop. This can make the code that much harder to read and decipher what its actual purpose is.
3. Which technique can help us solve callback hell?
To try and not use nested functions where possible but instead give each a meaningful name and declare them at the top level, making it easier to modularize the overall code. Adding comments where appropriate will also make the program easier to read. Other techniques are async await syntax, where we actually declare an async function in JavaScript and it knows to expect the possibility of the await keyword being used to invoke asynchronous code. This is used in conjunction with Promises, which is yet another technique used to solve callback hell, where Promise objects represent the eventual completion (or failure) of an asynchronous operation and its resulting value.