Asynchronous Programming in JavaScript: async and await

in javascript •  7 years ago  (edited)

Instead of using promises for async tasks, you can do the same thing using the new async and await keywords in JavaScript.

Consider this method:

    main(a) {
        return Promise.resolve(a)
        .then(a => asyncTaskA(a))
        .then(b => asyncTaskB(b))
        .then(c => asyncTaskC(c))
        .catch(e => console.log(e))
        .finally(() => cleanUp());
    }

You can rewrite it much simpler in this way:

    async main(a) {
        try {
            let b = await asyncTaskA(a);
            let c = await asyncTaskA(b);
            return await asyncTaskA(c);
        } catch (e) {
            console.log(e)
        }
        cleanUp();
    }

This is certainly a more elegant and simpler approach for writing async code.

It should be remembered that await can only be used inside a function that has been marked with the async keyword. This keyword shows that the function returns a promise and thus is an async function.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

thanks for interesting codes .. I'd like to know more also about javascript .. please insert the result window ,too
it will help us to understand better
thanks

Thank you. This is just to show how the keywords can be used, it's not an actual program.

ok then.. thanks anyway

sir waiting for your idioms tutorial

عالی

Are you javascript programmer ?

I use JavaScript for most of my programming jobs.

Is the Idioms tutorial end?

Yes, for now.

I don't know ever you share technology tutorial.After many idioms tutorial .Now we got programming tips from you.
But is it stop now?

Yes, I have decided to move on to other topics.

Now like Idiom tutorials, we will learn Java.

Just don't confuse JavaScript with Java. They are somewhat similar, but inherently different.

I really want to learn about JavaScript. But I think I have to study hard to see the codes. ha ha ha
thank you for sharing @ghasemkiani. You are very genius

Thanks. Learning JavaScript is relatively easy and very rewarding, because nowadays JavaScript is used for various kinds of programming.

thank you for supporting me, my brother @ghasemkiani. I will try it ;)

ممنون.شما که جاوا اسکریپت تجربه خوبی دارید مطالب تو این زمینه میتونه خیلی مفید باشه.ممنونم

Sir Are you javascript programmer ?

Well, I use JavaScript for many of my programming works.

i got it sir.... thanks for your reply....

Thanks for sharing the coding information.

Happy to your continue sharing

Helpful post aghai ghasim . Keep it up.

thank you

thanks for this knowledgeable post

Nice post it is informative.

Helpful post.

Assalamoalaikum dear

Your work is well,,,,, you are very intellengt writer,,,,, i hope your success is very soon,,, i support you #Alltime but i want to you support me,,,thank you for sharing with #Us,,,

great work my friend... you are doing a great job....

nice post it is always interesting

Its really nice and wonderful stuff steeming @ghasemkiani
I regularly support and reply.

Its a dynamic programming language. I like javascript programming :)

Great post dear bro im respect your work good job carry on dear @ghasemkiani

Its very useful post for all steemians . Very good analysis.
Thanks for sharing good post
Stay blessed

resteemed and upvote.

Thank you very much for your knowledge. Good luck always for you @ghasemkiani.

Another great tutorial @ghasemkiani.

Nice post.I respect you very much because you contribute to steemit.I will do activities like you.I would like to extend the steemit.

I will try to do as you say @ghasemkiani

javascript program also great.
Are you javascript programmer?

Helpful post....

Amazing post dear

keep it up i am learning

Amazin post dear

Very informative blog.

I appreciate your technology..Thanks for sharing this post..Carry on my dear.

great writing dear

1120.jpg