Sumit | Javascript + React
Sumit | Javascript + React

@sumitsaurabh927

7 Tweets 1 reads Apr 05, 2023
Async and await are powerful tools in the arsenal of any JavaScript developer.
But even experienced developers can struggle with these concepts.
Here's a simple explanation:
πŸ‘‰ Async keyword:
This keyword tells Javascript that the function being declared is an async one.
It is usually paired with the await keyword (inside the function)
The use of the async keyword means that the function always returns a promise.
And within an async function, we use await keyword to make Javascript wait till the promise is settled.
I've already explained promise settling & am presuming you've read that thread.
If you haven't, please go through it here:
Let's now discuss await
⚑ Await keyword:
As the name suggests, it has something to do with waiting.
πŸ•‘ This keyword makes Javascript wait till the promise has settled & returned.
It is like telling Javascript to pause execution till the promise has settled & resume when the promise has settled.
πŸ’‘ Because async functions & promises complement each other naturally, the keywords 'async' and 'await' are usually used in pairs.
But as shown above, we can have an async function without the 'await' keyword.
Use of these two keywords makes asynchronous code look synchronous.
β›” And like promises have .then and .catch methods to handle promise settlement, we have the try-catch block in async functions.
πŸ›‘οΈ They try-catch block essentially tells Javascript to 'try' executing the async function, return result but if there's an error, 'catch' it.
I write threads daily on web dev so if you enjoyed this thread, please:
βœ… Follow me @sumitsaurabh927 for more of these.
❀️ Each like, every comment of y'all motivates me to come up with awesome new stuff!
βœ… RT the tweet below to show me some love.

Loading suggestions...