🔥 Learn async/await (very basics) here 👇
you quickly fall into words like
Synchronous & Asynchronous code
These things aren't easy to understand in one go.
Complex theories gatekeep beginners.
So, we will only learn about practical stuff.
Let us first learn about "Promises"
In the below snippet, what we are intending is to
and then output
But the below snippet outputs "Done Last" first.
and then print
It accepts a function as the only argument.
The function receives few params. 2 of them being
resolve accepts arguments.
These arguments later become the params in the .then() function.
So, the .then() function runs only after the promise is resolved.
Well, don't create a Promise just for a "console.log after setTimeout".
This was just for explanation. 🙂
Now, here's the async/await part.
promise.then(() => console.log('Done Last.'))
can also be written as
await promise console.log('Done Last.')
Just like in the below snippet. It just works!
Wondering what's the
async part in the below snippet?
The await keyword only happens to work inside an async function.
An async function tells the compiler ahead of time that the function will be returning a Promise and will not have a value resolved right away.
I hope that gives a basic idea about what async/await is and what it does.
Here are two nice resources about it 🔥
If you spot a mistake, let everyone know 🙌
Comment below your better ways, and suggestions to improve this post. :)