 I recently read a medium post where the author claimed that using `async-await` is better than using `promises.` While this might hold good in general cases, I think that generalisation was too broad and doesn’t do justice to either `async-await` or `promises.` For somebody who is new to Javascript making sense of these and decide which one to use can be a challenge. In this post I will list down things that I have learnt about these and how I decide when to use what. I read somewhere that `async-await` is syntactical sugar for using `promises`. So before getting to know `async-await` or deciding on which approach to you use make sure that you have a better understanding of promises. I captured my learnings in the post [Understanding promises in Javascript](https://hackernoon.com/understanding-promises-in-javascript-13d99df067c1). If you do not have time to go through the full article checkout the thumb rules that I follow. ### Thumb Rules for using Promises  1. Use promises whenever you are using asynchronous or blocking code. 2. `resolve` maps to `then` and `reject` maps to `catch` for all practical purposes. 3. Make sure to write both `.catch` and `.then` methods for all the promises. 4. If something needs to be done in both the cases use `.finally` 5. We only get one shot at mutating each promise. 6. We can add multiple handlers to a single promise. 7. The return type of all the methods in `Promise` object whether they are static methods or prototype methods is again a `Promise` 8. In `Promise.all` the order of the promises are maintained in values variable irrespective of which promise was first resolved. * * * Once you have wrapped your head around promises checkout `async await.` It helps you to write code that is much more readable. When not used properly it has its downsides. You can read [Understanding async-await in Javascript](https://hackernoon.com/understanding-async-await-in-javascript-1d81bb079b2c) to checkout my learnings. If you do not have time to read the full article checkout my thumb rules. ### Thumb Rules for async-await Here are a list of thumb rules I use to keep my head sane around using `async`and `await` 1. `aync` functions returns a promise. 2. `async` functions use an implicit Promise to return its result. Even if you don’t return a promise explicitly `async` function makes sure that your code is passed through a promise. 3. `await` blocks the code execution within the `async` function, of which it(`await statement`) is a part. 4. There can be multiple `await` statements within a single `async` function. 5. When using `async await` make sure to use `try catch` for error handling. 6. Be extra careful when using `await` within loops and iterators. You might fall into the trap of writing sequentially executing code when it could have been easily done in parallel. 7. `await` is always for a single promise. 8. Promise creation starts the execution of asynchronous functionality. 9. `await` only blocks the code execution within the `async` function. It only makes sure that next line is executed when the `promise` resolves. So if an asynchronous activity has already started then `await` will not have an effect on it. * * * ### So should I use promises or async-await The answer is that we will use both. Following are the thumb rules I use to decide when to use `promises` and when to use `async await` 1. `async function` returns a `promise.` The converse is also true. Every function that returns a `promise` can be considered as `async function` 2. `await` is used for calling an `async function` and wait for it to `resolve` or `reject`. 3. `await` blocks the execution of the code within the `async` function in which it is located. 4. If the output of `function2` is dependent on output of `function1` then I use `await`. 5. If two functions can be run in parallel create two different `async functions` and then run them in parallel. 6. Two run promises in parallel create an array of promises and then use `Promise.all(promisesArray)` 7. Every time you use `await` remember that you are writing blocking code. Over the time we tend to neglect this. 8. Instead of creating huge `async functions` with many `await asyncFunction()` in it, it is better to create smaller `async functions.` This way we will be aware of not writing too much of blocking code. 9. Another advantage of using smaller `async functions` is that you force yourself to think what are the async functions that can be run in parallel. 10. If your code contains blocking code it is better to make it an `async`function. By doing this you are making sure that somebody else can use your function asynchronously. 11. By making async functions out of blocking code, you are enabling the user who will call your function to decide on the level of asynhronicity he wants. Hope this helps you decide easily when to use promises and when to use `promises` and when to use`async-await` If you liked this article and would like to read similar articles, don’t forget to clap. There is a limit of 50 claps on medium. Do check it out. <hr/> ### Other Related Articles 1. [Understanding Promises in Javascript](https://steemit.com/utopian-io/@gokulnk/understanding-promises-in-javascript) 2. [Understanding async-await in Javascript](https://steemit.com/javascript/@gokulnk/understanding-async-await-in-javascript) 3. [Understanding npm in Nodejs](https://steemit.com/javascript/@gokulnk/understanding-npm-in-nodejs)
author | gokulnk |
---|---|
permlink | should-i-use-promises-or-async-await |
category | javascript |
json_metadata | {"tags":["javascript","promises","async-await","thumbrule"],"image":["https://cdn-images-1.medium.com/max/2000/1*wBiJ82CJVPCyymYYzN1I9Q.png","https://cdn-images-1.medium.com/max/1600/1*0mBlni5vsYZE2wFzfVv8EA.png"],"links":["https://hackernoon.com/understanding-promises-in-javascript-13d99df067c1","https://hackernoon.com/understanding-async-await-in-javascript-1d81bb079b2c","https://steemit.com/utopian-io/@gokulnk/understanding-promises-in-javascript","https://steemit.com/javascript/@gokulnk/understanding-async-await-in-javascript","https://steemit.com/javascript/@gokulnk/understanding-npm-in-nodejs"],"app":"steemit/0.1","format":"markdown"} |
created | 2018-08-23 08:31:09 |
last_update | 2018-08-23 08:31:09 |
depth | 0 |
children | 3 |
last_payout | 2018-08-30 08:31:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 49.924 HBD |
curator_payout_value | 14.704 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 5,655 |
author_reputation | 17,871,219,215,380 |
root_title | "Should I use Promises or Async-Await" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 69,101,184 |
net_rshares | 44,086,324,246,812 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
cmtzco | 0 | 4,095,221,710 | 9% | ||
desmonid | 0 | 1,766,432,143 | 9% | ||
cryptohustlin | 0 | 8,904,599,223 | 9% | ||
alexpmorris | 0 | 182,292,921,413 | 62.5% | ||
williambanks | 0 | 1,635,944,905 | 100% | ||
merej99 | 0 | 87,395,428,054 | 100% | ||
bridgetbunchy | 0 | 126,086,342,211 | 25% | ||
bofadeez | 0 | 402,394,719 | 9% | ||
cryptomancer | 0 | 57,895,298,374 | 25% | ||
badassbarbie | 0 | 8,992,492,708 | 100% | ||
eileenbeach | 0 | 2,940,746,125 | 9% | ||
ghostwriter | 0 | 16,733,115,811 | 100% | ||
decebal2dac | 0 | 4,557,414,280 | 62.5% | ||
michellectv | 0 | 123,664,531,821 | 62.5% | ||
baldos | 0 | 4,377,670,074 | 62.5% | ||
tdiv7653 | 0 | 1,202,742,125 | 100% | ||
shellyduncan | 0 | 82,073,499,235 | 62.5% | ||
upheaver | 0 | 976,584,883 | 5% | ||
urs | 0 | 33,889,633,064 | 62.5% | ||
yehey | 0 | 7,844,364,496 | 10% | ||
minnowpond | 0 | 56,970,451,508 | 9% | ||
minnowpond1 | 0 | 972,757,165 | 9% | ||
cifer | 0 | 3,608,013,863 | 73% | ||
thewindowflower | 0 | 58,476,797 | 9% | ||
espoem | 0 | 8,337,985,935 | 8% | ||
alfajri78 | 0 | 1,650,249,769 | 100% | ||
postpromoter | 0 | 40,991,117,941,648 | 33.23% | ||
bobinson | 0 | 1,670,083,044 | 15% | ||
danile666 | 0 | 14,339,503,491 | 62.5% | ||
ering | 0 | 1,977,198,558 | 9% | ||
harmonicliving | 0 | 2,748,105,591 | 9% | ||
sammyb | 0 | 3,982,263,708 | 9% | ||
schulbz | 0 | 794,261,126 | 9% | ||
fortunex | 0 | 3,109,843,261 | 99% | ||
thedrayness | 0 | 1,382,815,688 | 9% | ||
thevark | 0 | 3,173,827,899 | 9% | ||
drdeepdick | 0 | 112,944,104 | 9% | ||
goldrooster | 0 | 25,807,157,928 | 100% | ||
unholyleaf | 0 | 898,474,159 | 9% | ||
shamrock017 | 0 | 467,236,565 | 9% | ||
mshahabi | 0 | 350,192,718,908 | 66.2% | ||
drguy | 0 | 3,834,555,936 | 9% | ||
blockchaindaily | 0 | 6,925,474,443 | 9% | ||
danai | 0 | 10,909,184,791 | 100% | ||
realseb | 0 | 15,731,007,882 | 62.5% | ||
andrecarothers | 0 | 25,335,386,841 | 9% | ||
indopeople | 0 | 521,872,165 | 100% | ||
wirednkod | 0 | 131,401,065 | 25% | ||
upboater | 0 | 607,717,131,483 | 62.5% | ||
arcanebinder | 0 | 239,059,505 | 100% | ||
jpmay | 0 | 1,466,589,306 | 100% | ||
snackplus | 0 | 59,097,729,363 | 20% | ||
theshadowek | 0 | 349,514,013 | 100% | ||
m3ik3 | 0 | 221,732,588 | 50% | ||
apt-get | 0 | 224,700,165 | 50% | ||
kleinheim | 0 | 225,580,352 | 50% | ||
v6476721 | 0 | 513,293,739 | 100% | ||
mexa | 0 | 504,098,492 | 100% | ||
den9 | 0 | 511,133,882 | 100% | ||
loft505 | 0 | 499,288,425 | 100% | ||
yarondrei | 0 | 498,621,083 | 100% | ||
annetlitvin | 0 | 499,400,269 | 100% | ||
olegjadkov | 0 | 508,891,643 | 100% | ||
lordofreward | 0 | 428,029,957 | 0.75% | ||
hronys | 0 | 492,279,851 | 100% | ||
council | 0 | 636,440,940 | 10% | ||
alofe.oluwafemi | 0 | 2,137,850,039 | 100% | ||
desp-er | 0 | 500,077,843 | 100% | ||
gormogon | 0 | 5,491,209,601 | 1% | ||
emilimxitaryan | 0 | 501,311,513 | 100% | ||
jorikmxitaryan | 0 | 498,094,830 | 100% | ||
johnfogel | 0 | 498,187,154 | 100% | ||
hranushdavdyan | 0 | 519,451,239 | 100% | ||
friedcheck | 0 | 510,338,060 | 100% | ||
efficientseptum | 0 | 519,451,239 | 100% | ||
iridiumnonagon | 0 | 510,338,060 | 100% | ||
gracefulestrogen | 0 | 513,375,786 | 100% | ||
gaws | 0 | 501,224,880 | 100% | ||
zip512 | 0 | 907,587,932 | 9% | ||
hammerthreadbare | 0 | 504,262,607 | 100% | ||
patterpavo | 0 | 501,222,058 | 100% | ||
danilkramov | 0 | 501,224,880 | 100% | ||
ron770 | 0 | 989,812,745 | 100% | ||
daileyfre | 0 | 510,338,060 | 100% | ||
gunterjos | 0 | 516,413,513 | 100% | ||
gartiv88 | 0 | 528,564,419 | 100% | ||
gravityenteral | 0 | 492,111,700 | 100% | ||
imperfectvital | 0 | 504,262,607 | 100% | ||
appleabsurd | 0 | 510,338,060 | 100% | ||
luffnoisy | 0 | 507,300,333 | 100% | ||
movementfrugal | 0 | 508,798,236 | 100% | ||
bungxebec | 0 | 510,338,060 | 100% | ||
anycases | 0 | 507,300,333 | 100% | ||
resultgame | 0 | 513,372,896 | 100% | ||
wagglergranted | 0 | 507,297,477 | 100% | ||
royjim | 0 | 225,569,000 | 50% | ||
gluonoff | 0 | 507,300,333 | 100% | ||
targetfile | 0 | 510,335,187 | 100% | ||
chewycareless | 0 | 525,526,693 | 100% | ||
kiraluchkova | 0 | 507,300,333 | 100% | ||
lisicka | 0 | 507,300,333 | 100% | ||
lapin124 | 0 | 490,220,869 | 100% | ||
witnessupdate | 0 | 18,823,337,373 | 62.5% | ||
pi10000 | 0 | 72,651,044,264 | 62.5% | ||
hitmanchoi | 0 | 57,849,057 | 20% | ||
tamilaisaeva | 0 | 504,259,768 | 100% | ||
andreiakilov | 0 | 510,338,060 | 100% | ||
telephonewhy | 0 | 501,222,058 | 100% | ||
towheelocate | 0 | 498,184,349 | 100% | ||
redirectunkind | 0 | 519,448,315 | 100% | ||
liquidmeat | 0 | 498,187,154 | 100% | ||
snailrepeat | 0 | 501,222,058 | 100% | ||
desktopcarrots | 0 | 504,262,607 | 100% | ||
hastypeacock | 0 | 531,602,146 | 100% | ||
kindlytwist | 0 | 498,187,154 | 100% | ||
drkcyd | 0 | 121,638,210 | 77% | ||
shamefulhasty | 0 | 498,184,349 | 100% | ||
seevalhrona | 0 | 498,184,349 | 100% | ||
insidescraper | 0 | 501,224,880 | 100% | ||
superpromoter | 0 | 138,061,723,881 | 44% | ||
spidervariety | 0 | 513,372,896 | 100% | ||
pyroxenebattery | 0 | 519,448,315 | 100% | ||
bohrbowling | 0 | 525,523,734 | 100% | ||
listlabcoat | 0 | 507,300,333 | 100% | ||
zhilin8 | 0 | 507,211,622 | 100% | ||
anryper | 0 | 498,100,036 | 100% | ||
steem.doctor | 0 | 228,330,894 | 10% | ||
spotted | 0 | 35,802,142,468 | 75% | ||
happy33 | 0 | 74,877,127 | 100% | ||
lost-and-found | 0 | 225,734,503 | 50% | ||
jacob-miller | 0 | 497,348,485 | 100% | ||
oh659094 | 0 | 106,699,319 | 100% | ||
limefury | 0 | 1,435,470,465 | 100% | ||
voter000 | 0 | 23,037,238,902 | 9% | ||
aicurator | 0 | 717,393,053,129 | 50% | ||
agromeror | 0 | 22,421,284,071 | 100% | ||
paelladiamond | 0 | 498,100,036 | 100% | ||
englishbracket | 0 | 513,286,013 | 100% | ||
dyogramsponson | 0 | 504,174,427 | 100% | ||
ovariangroups | 0 | 517,860,912 | 100% | ||
u25b2x | 0 | 18,742,043,831 | 9% | ||
allinbox | 0 | 259,088,483 | 100% | ||
jamzmie | 0 | 703,125,447 | 100% | ||
brayan007 | 0 | 595,406,977 | 100% | ||
aljager78 | 0 | 516,929,501 | 100% | ||
dzhoshu | 0 | 513,877,564 | 100% | ||
oleg.domov | 0 | 504,740,416 | 100% | ||
ivan.siko | 0 | 507,781,021 | 100% | ||
coinnog | 0 | 16,744,999,167 | 100% | ||
aviv.world | 0 | 2,109,799,270 | 100% | ||
babarmallah786 | 0 | 540,971,491 | 100% | ||
tertkurt | 0 | 595,421,179 | 100% | ||
leysonrye0724 | 0 | 104,284,065 | 100% | ||
amzar123 | 0 | 6,662,025,505 | 100% |
You have recieved a free upvote from minnowpond, Send 0.1 -> 10 SBD with your post url as the memo to recieve an upvote from up to 100 accounts!
author | minnowpond |
---|---|
permlink | re-should-i-use-promises-or-async-await-20180823t164736 |
category | javascript |
json_metadata | "" |
created | 2018-08-23 16:47:36 |
last_update | 2018-08-23 16:47:36 |
depth | 1 |
children | 0 |
last_payout | 2018-08-30 16:47:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 144 |
author_reputation | 13,239,048,956,578 |
root_title | "Should I use Promises or Async-Await" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 69,142,216 |
net_rshares | 0 |
You got a 33.23% upvote from @postpromoter courtesy of @gokulnk! Want to promote your posts too? Check out the [Steem Bot Tracker website](https://steembottracker.com) for more info. If you would like to support the development of @postpromoter and the bot tracker please [vote for @yabapmatt for witness!](https://v2.steemconnect.com/sign/account-witness-vote?witness=yabapmatt&approve=1)
author | postpromoter |
---|---|
permlink | re-gokulnk-should-i-use-promises-or-async-await-20180823t095406532z |
category | javascript |
json_metadata | {"app":"postpromoter/2.1.0"} |
created | 2018-08-23 09:54:06 |
last_update | 2018-08-23 09:54:06 |
depth | 1 |
children | 0 |
last_payout | 2018-08-30 09:54:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 391 |
author_reputation | 12,722,616,650,811 |
root_title | "Should I use Promises or Async-Await" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 69,107,301 |
net_rshares | 0 |
Congratulations @gokulnk! You have completed the following achievement on Steemit and have been rewarded with new badge(s) : [](http://steemitboard.com/@gokulnk) Award for the number of upvotes <sub>_Click on the badge to view your Board of Honor._</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> > Do you like [SteemitBoard's project](https://steemit.com/@steemitboard)? Then **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
author | steemitboard |
---|---|
permlink | steemitboard-notify-gokulnk-20180904t041341000z |
category | javascript |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2018-09-04 04:13:42 |
last_update | 2018-09-04 04:13:42 |
depth | 1 |
children | 0 |
last_payout | 2018-09-11 04:13:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 668 |
author_reputation | 38,975,615,169,260 |
root_title | "Should I use Promises or Async-Await" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 70,250,228 |
net_rshares | 0 |