0% found this document useful (0 votes)
2 views10 pages

Asynchawaait

asynch await

Uploaded by

suchit kapale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Asynchawaait

asynch await

Uploaded by

suchit kapale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

wahabzafar.dev@gmail.

com
[email protected]

What is Async/Await ?
The async keyword is used to declare an
asynchronous function.

It returns a Promise, and within the


function. . .

You can use await to pause the


execution of the code until a promise is
resolved.

Practical Examples
[email protected]

Here we Have

1st Function returning


a String

2nd Function
returning a String

3rd Function
returning a String

Here we have Call function


in which we are calling the
above functions and
logging their values

Output
[email protected]

Making the Code


Asynchronous
Now, we will make the code Asynchronous by adding a
Promise in Second Function, which will resolve after 3
seconds

Returning a
Promise
After 3
seconds
[email protected]

Now by doing this

We’ll have a
pending promise
that’s not resolved
at runtime.

Output
[email protected]

Adding Async/Await
Now, we will use Async/Await in the Call Function to make
the code wait and get the value from the second function

Use Async on
Call function

Use Await
inside Async
function
[email protected]

Await the promise


to get the resolved
value

Output
[email protected]

Hope it’s Cleared!


One Real Life Example to make the concept
much stronger

Example
[email protected]

Real Life Example


Here, we’re getting data from an API, so we use
async/await to wait for the response and handle it
efficiently.

Async keyword Getting API using fetch

We used two await here


First waiting for API response
Second to convert the response into
JSON.
[email protected]

Hope it
Helped you!
Follow for more useful Content!!

If this guide helped you, let


me know in the comments!

You might also like