DEV Community

Cover image for I Promise this is a practical guide to Async / Await

I Promise this is a practical guide to Async / Await

Michael "lampe" Lazarski on February 12, 2019

With ES8 we got another way to write code that is async in a more readable way then callback's its called Async / Await. With ES6 we already got Pr...
Collapse
 
namirsab profile image
Namir

Hey Micha!
Nice post!

There is a better way to chain promises one after another. You can just return a promise within a then callback, like this:

resolveAfterXSeconds(2000)
  .then((ms) => { 
    console.log('promise in the first then');
    console.log(`resolved after ${ms}`);

    return resolveAfterXSeconds(1000);
  }).then((ms) => { console.log(`resolved after ${ms}`) });

Enter fullscreen mode Exit fullscreen mode

This way you can avoid the similarity of the "callback" hell, and you get exactly the same result in a more readable way.
First of all execute the code after 2 seconds, and then execute the code after 1 seconds.

PS: Btw, the name of the function should be resolveAfterXMilliseconds

Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski • Edited

Hey Namir,
Thanks.

yes, this is another solution is to chain the promises. Like I have written that the given first example is just an example I have seen and should be more an anti-pattern. If you chain a lot of promises, so what you want to run your code in a sequential manner then I think that Await / Async is the even more readable solution.

time = await resolveAfterXSeconds(2000);
console.log(`resolved after ${time}`);
time = await resolveAfterXSeconds(1000);
console.log(`resolved after ${time}`);
Collapse
 
vbarzana profile image
Victor A. Barzana

Lol wanted to say the same 😉 great article btw

Collapse
 
nestedsoftware profile image
Nested Software

This code is truly parallel.

All of the code you've shown is running either concurrently (allowing other code to run while waiting for I/O) or sequentially (one after the other).

The term parallel refers to code that is running at the same time. If you have two pieces of code running in parallel, it means both are running simultaneously on two different CPUs or CPU cores.

Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski

You are correct.
The example maybe I chose is not the best.

I should explain it better.

It is of course not parallel on the CPU level.
For that, you need web workers.

What I meant there is that if you call for example 2 API calls they can be called in parallel. You don't need to wait for one to finish to call another one.

Thanks for making that clear! Good job!

Have a nice day!

Collapse
 
quozzo profile image
Quozzo

Sorry but I stopped reading after I saw 4 grammatical errors in the first sentence. It should read...
"With ES8 we got another way to write code that is async in a more readable way than callbacks, it's called Async / Await."

Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski

What should I now do with this comment?

How does this comment help?

And for what are you sorry?

A sorry followed by but is...

Collapse
 
paddyh4 profile image
Pradeep Chavan

Thank You for finally getting my fundamentals on async/await clear. I am Pradeep Chavan, based in India.I develop software for Tailors and Designers. I like to get in touch with you regarding the design and the working of my project, developed using php, mysql and html5, css3. My mode of communication is usually through email .please send me your email .
Thank You.
Rgds,
Pradeep Chavan.
email:pradeepdchavan@gmail.com
mob:+91 7400158620