Use of await in async functions
await
is used to pause the execution of an asynchronous function and wait for a Promise
to be resolved. It can only be used within an async
function. The await
keyword allows you to wait for the result of an asynchronous operation before moving on to the next line of code, making the code appear more like synchronous code.
Do we need to wait for all asynchronous codes🤔?
Not necessarily. In many cases, asynchronous code can run independently and does not require the main thread to wait for it to complete. This allows for the program to continue executing other tasks while the asynchronous operation is being performed in the background. However, there are cases where you may want to wait for the completion of an asynchronous operation, and in those cases, you can use the await
keyword to pause the execution of the code until the Promise is resolved.