await
is a keyword that can be used inside an async function to pause the execution of the function until a promise is resolved.
Let's take the same example of launching a rocket and see how we can use the await
keyword to make the code look synchronous.
Here in the launchRocket
function, we use the await
keyword to pause and wait for specific tasks to complete. We start by waiting for rocket parts, then for the rocket assembly, and so on, ensuring each step finishes before moving to the next. This sequence continues until we launch the rocket.
Another point to note is that the await
keyword only affects the code inside the async function. The code outside of it will keep running. For example, if we add a console.log
statement after the launchRocket
function call, it will be executed immediately after the function call, not after the launchRocket
function is finished.