Please. Been trying to find a clear explanation as to why Async/Await > Promises or so lots of devs say.
For further actions, you may consider blocking this person and/or reporting abuse
Please. Been trying to find a clear explanation as to why Async/Await > Promises or so lots of devs say.
For further actions, you may consider blocking this person and/or reporting abuse
Latest comments (24)
A
Promiseis a "handle" to a value that may or may not already be available.It's better than a callback, because you can start the asynchronous process first, and think about what you will do with the outcome of it later.
but what is
await? It allows you to wait until the value of a promise is available within synchronous-looking code, with all the usual imperative constructs.How is this achieved? When you execute "await", your function pauses, and allows other things to run.
Then, when the awaited result is ready, the function continues execution from that point onward, as if "await" was just a function that took a long time.
Think of it like the code before the
awaitand the code after theawaitare two separate functions, just keeping the same variable scope.That's why it can only be used in
asyncfunctions: since the function doesn't "finish" (return the actual value and end forever) when called, but later, it also must return a Promise. Theasyncannotation is just so that people are not confused why the function returns a Promise when called instead of what was actually passed toreturnstatements in it.Understanding that it's
Promises underneath, and that eachawaitstops the function and puts it in a queue tells us how we should optimize for good performance:The same, but without
thenandpush(previous was written to be similar to the bad example)Gotchas
All
throws in a function markedasyncare rejections, never throws from the initial function call.This outputs
But this isn't always true for all Promise-returning functions!
Because async/await is actually one level higher than
Promise. An async function is not aPromisebut a() => Promise. In other words, an async function is a lazyPromise.Note that we kind of hit a dead-end above. Even though we've created
p0to store the Promise, we cannot rerunp0. The most we can do is attach morethens. We cannot cancel it either.We still cannot cancel it, but at least we can rerun it.
A more detailed explanation can be found in the Wiki of Fluture.
I have a reputation to uphold here: stackoverflow.com/a/1638961/31899 So let me give it a shot. Ahem! :)
I am your faithful dog JS. I love you with all my heart, but normally when you go off to school. I go do other stuff. I find React the cat and bark at him. That's fun because he's getting really old. I nap. I eat. I drive the dreaded "mail man" away from our house. Then, when you get home again, we play! Hooray!
But today, you said, "Await me and I will give you this bone." So I have not moved. I am a good dog. I am a very good dog. I will stay here and not do anything else until you get back and then I will get the bone and we will play!
That, is Async/Await for a five year old.
Love it! :D
LMAO I love this!!!
Async:
Promise:
Imagine you drop a pillow, that pillow falls, right?
Then imagine if you make that pillow async and in the middle of it you say await, the pillow stops in the middle of the air and you can do something else like change the pillow cover, only when you are done, the pillow continues falling.
Async/await isn't better than promises. It's more imperative. So naturally people who prefer imperative code will prefer async/await over promises.
I'll throw my hat into the ring.
Let's say you're running a race, and there are water stations set up along the way. You can keep running down the main path, or hop off to the side to one of the water stations. Some of them have lines, and you have to wait your turn before you can get water and go back to the main path. To
awaitmeans to be one of the people waiting in line, because you're *wait*ing for the person in front of you to finish what they're doing. For a minute, the two runners are joined together and can only go as fast as the first of them. Waiting in line makes it so that other people can queue up behind us (we have to be markedasync, which means other people canawaitus before we canawaitsomeone else).In terms of code, this makes it much cleaner and straightforward, as I think Davyd has demonstrated.
Code can be synchronous, where a line of code has to be completed before you can move on to the next one, and asynchronous, where the program will move on to the next line without waiting for the previous line to finish.
Async/await makes asynchronous code feel synchronous, aka won't move to the next line until the previous line finishes execution, whilst in fact it's asynchronous in the background. It's much easier to read code line by line than having to parse the cognitive complexity that promises introduce.
Actually this is a very good question that shows the problem with the way programmers learn to code (no judgment here).
If you understand what was before promises, and understand the fundamentals of JS (event loop etc...)
you will have no problem understanding await/async, but programmers always skip to the last piece of technology instead of learning the basics first.
the original question was already answered so I just wanted to add this.
I think the newbies (such as myself) don't really know any better and everything they look up features the latest and greatest shiny new thing.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.