Async JavaScript — Callbacks, Promises, And Async Await
What is Async JavaScript and how to use it?
Parallelism is the utilization of multiple threads in an operating system. Routines can run at the same time regardless of execution order.
JavaScript, however, is single-threaded and only one line of code can be executed at any given time. Many functions are provided by JavaScript host environments that allow you to schedule asynchronous actions. In other words, actions that we initiate now, but they finish later. For instance, one such function is the setTimeout
function. So how do we achieve Async execution of code in JavaScript? These 3 come to our rescue:
- Callbacks
- Promises
- Async Await
Let’s look at each one of them, their pros and cons, and how we can use them effectively while coding.
Callbacks
A callback is a function passed as an argument to another function. Let’s look at a use case and see how we can use callbacks in it.