JavaScript async & await

 

What is async & await ?

In JavaScript, the async and await keywords are used together to handle asynchronous operations in a more synchronous and readable manner. Introduced In ECMAScript 2017

  • async and await make promises easier to write
  • async makes a function return a Promise
  • await makes a function wait for a Promise

async

The async keyword is used before a function declaration to indicate that the function is asynchronous. It allows the function to return a Promise implicitly. Inside an async function, you can use the await keyword to pause the execution of the function until a Promise is resolved or rejected.

await

The await keyword can only be used inside an async function. It is placed before a Promise or an expression that returns a Pronmise. When encountering an await statement, the execution of the async function is paused until the Promise is settled (resolved or rejected). While waiting for the Promise to be resolved, the function's execution is suspended, allowing other code to run in the meantime.

Post a Comment

Previous Post Next Post