Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
olback109815yThink of every async function as its own thread. And await just waits for it to finish.
Probably a very flawd explanation but hey 🤷♂️.
Just to clarify: Javascript is still very much single-threaded. -
An async function promises you to return a value someday.
You can either await that result or not. -
There s a async await explanation by a guy who worked on it in C# on channel9! from MS. Has a nice graphic explaining it and all that. See if you can find it. I’m on phone my man or I could have found it for you. Good luck !
-
-
A promise will call a callback function when it is resolved at some point. It may or may not pass a value as a parameter.
You can register a callback function with the "then" method of the promise, or with the await keyword. Assigning a variable to the awaited promise sets that variable to the value the promise resolved with.
If you use the await keyword, the function you use it in must be marked with the async keyword. This makes it return its own promise, which will resolve when the awaited promise inside resolves and everything after the awaited promise finishes. If the async function returns a value, that will be the value its promise resolves with, which can itself be awaited in another async function.
Simple, right? -
You ask a friend (the async function) to buy a box of chocolate (the result) for you. You give him some money (a parameter). He gives you a piece of paper (the promise object) and you tape it to your front door. You both agree that he will follow any directions you write on there when he's done. He leaves and you continue doing your own work. To let him know what to do with the chocolates, you write directions (the callback function) on the piece of paper (registering it). After some time he has bought the chocolates (fulfilling the promise), reads the directions and does what you wrote on the paper (running the callback), which reads "Please put the sweets and the receipt in my mailbox (some variables). If you couldn't get any, please put my money back in there. Anyhow write an sms (set a flag)". After a while you check your smartphone (polling the flag that the callback sets) you see the message (the set flag) and you empty the mailbox: A box of chocolates which you eat right away.
-
hack64565yMike: Hey jhon can you do this task for me?
John: yeah sure whatever
1 seconds later Bob asks to mike: Hey mike what are you doing?
Mike: I am doing my job leave me alone.
(At that point mike and jhon does theor job in parallel)
Mike: hey jhon can you do this task for me? I will await you.
Jhon: yeah sure whatever.
1 seconds later Bob asks to mike: Hey mike what are you doing?
Mike: I am awaiting jhon to finish his task because it blocks me.
As you may probably understand first one is calling async function without await and they run "like" they are on two different threads. Second example is you just wait that async function to complete its execution. -
-AJ-2325y@sandeepbalan go to your room and play with your toys while go i down the freezer. I PROMISE it’ll be worth it.
.....
@sandeepbalan come over here, I have your ice cream ready.
Explain js promises, async/await like i am 5.
joke/meme