Async Js
Async Js
5
Async functions vs sync functions, real use of callbacks
JS Browser architecture
Promises
Async await
fl
Async functions vs sync functions
Total time
5+
5 minutes
Async functions vs sync functions
Lets build some intuition
Total time
5+
2 minutes
Async functions vs sync functions
Lets build some intuition
Total time
5+2
2 minutes
Async functions vs sync functions
Lets build some intuition
Total time
5 + 2 +10
10 minutes
Async functions vs sync functions
Lets build some intuition
Total time
5 + 2 +10 +20
= 37 mins
20 minutes
Async functions vs sync functions
Lets build some intuition
Total time
Total time
Total time
Total time
Total time
Total time
Total time
Total time
12 minutes
Everything is done!
Async functions vs sync functions
Lets build some intuition
Turn on gas(1s)
Delegate task
Total time
12 minutes
Even if you are single threaded (brain can do only one thing at a time), you can do things parallely by Delegating
You can also context switch between tasks if need be (the net time to do both the things would still be the same)
After 1 second,
remind me to run a function
Starts executing
the callback
http://latent ip.com/loupe
https://gist.github.com/hkirat/bd8ea48f26e4f2ba44204216377ba27c
fl
Good checkpoint
Timmy Simmy
Promises
Lets see the communication b/w these 2
Timmy Simmy
Promises
Lets see the communication b/w these 2
Timmy Simmy
callback
fi
Promises
Lets see the communication b/w these 2
Timmy Simmy
callback
Promises
Lets see the communication b/w these 2
Timmy Simmy
callback
callback()
Timmy Simmy
Timmy executes it
Promises
What does the code look like?
Promises
Ugly way
https://gist.github.com/hkirat/621d9168e39d99bcc14d767c912e9777
fi
Promises
https://gist.github.com/hkirat/75be07d3ea18f93cba907c85233b4217
Lets see how Timmy and Simmy talk
Timmy
Simmy
fi
Lets see how Timmy and Simmy talk
Timmy
Simmy
Lets see how Timmy and Simmy talk
Timmy
Simmy
fi
Lets see how Timmy and Simmy talk
Timmy
Simmy
fi
Lets see how Timmy and Simmy talk
Timmy
Simmy
Lets see how Timmy and Simmy talk
fi
Here’s a simple promise that immediately resolves
Promises
https://gist.github.com/hkirat/46580244f43ca2847cfd57664a3803b1
Promises
Try to marinate both the sides
1. Creating an async fn
2. Calling an async fn
Why even make it async Harkirat seems like you are just doing something
that can be done with a normal function
https://gist.github.com/hkirat/46580244f43ca2847cfd57664a3803b1
Why even make it async Harkirat seems like you are just doing something
that can be done with a normal function
https://gist.github.com/hkirat/ceac2c9198f1411ba8f5aea3ada769f3 https://gist.github.com/hkirat/9e00de2335d1ead90436adef68c9b2f9
Aynsc await syntax
Again, it is just syntactic sugar. Still uses callbacks/Promises under the hood
Makes code much more readable than callbacks/Promises
Usually used on the caller side, not on the side where you de ne an async function
fi
Aynsc await syntax
https://gist.github.com/hkirat/ceac2c9198f1411ba8f5aea3ada769f3 https://gist.github.com/hkirat/9e00de2335d1ead90436adef68c9b2f9
Aynsc await syntax
In fact, all three are very similar (becomes more manageable as you move to the right
https://gist.github.com/hkirat/9843537dfcaf48ea69df1ec4c4d9091d
https://gist.github.com/hkirat/9e00de2335d1ead90436adef68c9b2f9
https://gist.github.com/hkirat/ceac2c9198f1411ba8f5aea3ada769f3