Asynchronous Programming in JavaScript: async and await
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.
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
عالی
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.
Are you javascript programmer ?
I use JavaScript for most of my programming jobs.
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....