JavaScript is a popular programming language that started out as a single-threaded, synchronous language, meaning that one step runs after another step without waiting to fetch any external resources or perform any lengthy calculation or process. If the script requires such a resource or calculation, this synchronous behavior results in an error. This blocks all other processes in the queue from running whether or not they were dependent on the step that produced the error.

But some time ago, JavaScript introduced a new feature that made it possible to wait only for code that requires an external resource to load or a lengthy process to complete while processing and rendering the rest of the code. This asynchronous behavior is achieved by using callbacks or promises, which work at the function level.

What are callbacks and promises?

I’ll explain these concepts through the help of code. If you already know what callbacks and promises are, feel free to skip down to the top-level await section and example application.

#js #javascript

Streamline your JavaScript code with top-level await
2.25 GEEK