Awaiting the async…

Develoger
Develoger
Published in
2 min readJul 13, 2017

--

Have you ever wondered why do we need tickets for the metro ride?

There are many complex concepts in programming and even more ways to make them seem complicated.

Luckily async / await is easy to understand and I will explain it in just 1 sentence… Wait for it… wait for it… Go!

async function will always return a promise and await operator gives us the opportunity to wait for a promise within the function.

Demo

Everybody knows that jQuery ajax() method returns a promise. Thus I used it in this example.

Source

It should be simple now. When you enter the metro and buy that ticket, railroad company gives you the promise that train will arrive at specific time. The only thing that you need to do is to await for it…

Can You Use it?

Node.js introduced support for async / await from version 7.6.0
Browser support is horrible as usual (for new features). IE completely ignores it and Edge implemented it in version 15.
Babel is luckily there to help us Async to generator transform

Have in mind that await operator is tightly coupled with async function and that it can’t be used outside of it!

If you are reading this it means that you are curious and passionate about JavaScript and Front-End in general. That makes two of us!
Consider joining me on this adventure of discovery by following me here, clicking on that heart, or checking out my twitter account https://twitter.com/develoger

--

--