async function/promises vs try and catch
Hey, what is the difference between an async function/promise and a try and catch expression? To me, they both seem to be similar due to the error handling, i don't really get the use of the async function in this case, can someone please explain? Thanks in advance.
30 Replies
- an async function is a function that automatically returns a promise that resolves on success, or rejects when you throw an exception inside it
- a promise is a callback that will finish sometime in the future, at any point, and you either resolve it or reject it
- a try...catch block is a block that catches exceptions and lets you do some cleanup/logging with the exception
as you can see, they are unrelated and can't be compared
it's like comparing apples to steaks
Ah okay, I see, thank you. Off the top of my head, I can’t think of a use case for async functions that perhaps doesn’t link to backend. Would you be able to give an example please?
anything that handles requests for permissions
https://developer.mozilla.org/en-US/docs/Web/API/Serial <-- serial communication uses promises
Ah okay I’ll take a look at that. Thanks.
SetTimeout is the original Promise
it's the shoddy way to make something async-ish
but can be used to sorta emulate promises
No it def shouldn’t be used outside of how it should be used but it’s always the example to introduce Promises to beginners when I was trying to learn. OP said they couldn’t think of a use case for asynchronous functions that don’t connect to the backend , so it’s an example of an asynchronous function that doesn’t connect to the backend
i think he's talking about
async function
Ahhh I think you’re right. In that case , I’ve used async function for a demo project that used a Timer / Stopwatch . As an example
it can also be used to convert from callback to promise, for some things
for example, when handling cropping in js
one part of it is to obtain an usable image file from the canvas, which uses a callback
I’m not sure of the proper term but I was referring to the function with the keywords async and await in etc.
All of this is really complex haha, if I ever came across something that would benefit from the use of things like this I’d never think to use it due to how complex it seems. I’d always try to solve it with more basic functions. I don’t really know how to get out of that mindset
its not complex at all
the async/await is the easy mode of using promises
all you have to do is just have
async
before the name, and the return value resolves the promise
and whatever you returned will be set on the variable that received the value from await
ing
it really is that easy
promises are a lot more annoying
something else that uses promises is navigator.clipboard
for example, the writeText()
method resolves when it thinks that the text was set into the clipboard
and if it wasnt, or clipboard access was rejected, then it throws an exception
and the exception rejects the promiseI know and can see how it’s not similar to try and catch. But it really seems like that to me. It even seems like a bit of an if statement just written in a different way.
But I see, I’ll look more into this then, thanks
i cant see how it is like an if statement
I believe you said an async function returns a promise that resolves on success or rejects when you throw an exception. To me that seems like, if xyz, return promise, else, return exception
yes, but thats different from a promise
i mean, that may sound the same, but is very different
Ah okay. I can’t quite grasp this subject yet I don’t think.
you will if you try to use ir
it
Yeah for sure. But that’s where I had the issue with not knowing use cases. Obviously the ones you pointed out are use cases, but the clipboard one seems really niche, so it’d be harder to understand
well, it isnt as niche as you think
if you go to github and click the "copy file", it's using this api
im sure there are others, but they are trully niche ones
Oh okay I see. I’m not trying to sound dismissive of anything you’re saying, I just realised it might seem like that. I’m just trying to figure out the best route to go down for learning it you know.
the thing is ... there isnt a lot to learn
it is just confusing
that is all
there, you used promises and await
as long as you use this inside an async function, the
y
will be 5
Ohh I see, thank you
An easy way to think about
await
is when someone makes a promise to you, they are saying "wait for me to do this" so you have to wait- and when its a JS promise, its the same thing, async is happening at different times and you have to await
for the responsesthat is an easy way to think about it
also, promises can take 1 cpu cycle or 1000 million years
yep and you gotta wait no matter what 🙂
until the promise is
resolved
and then
you do stuff with the information that was promised to you
but if you catch
issues with the promised "work" then you need to deal with that information too
and if you really need to, finally
after you're done with everything and finished with the promise you made, then you cleanup to move onyou have to ... await
(if you want to go the easy route)
😉
oh yeah that's a useful way to think about it, thank you
no prob 🙂