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
ἔρως
ἔρως5d ago
- 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
snxxwyy
snxxwyy5d ago
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?
ἔρως
ἔρως5d ago
anything that handles requests for permissions https://developer.mozilla.org/en-US/docs/Web/API/Serial <-- serial communication uses promises
snxxwyy
snxxwyy4d ago
Ah okay I’ll take a look at that. Thanks.
clevermissfox
clevermissfox4d ago
SetTimeout is the original Promise
ἔρως
ἔρως4d ago
it's the shoddy way to make something async-ish but can be used to sorta emulate promises
clevermissfox
clevermissfox4d ago
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
ἔρως
ἔρως4d ago
i think he's talking about async function
clevermissfox
clevermissfox4d ago
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
ἔρως
ἔρως4d ago
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
snxxwyy
snxxwyy4d ago
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
ἔρως
ἔρως4d ago
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 awaiting 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 promise
snxxwyy
snxxwyy4d ago
I 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
ἔρως
ἔρως4d ago
i cant see how it is like an if statement
snxxwyy
snxxwyy4d ago
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
ἔρως
ἔρως4d ago
yes, but thats different from a promise i mean, that may sound the same, but is very different
snxxwyy
snxxwyy4d ago
Ah okay. I can’t quite grasp this subject yet I don’t think.
ἔρως
ἔρως4d ago
you will if you try to use ir it
snxxwyy
snxxwyy4d ago
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
ἔρως
ἔρως4d ago
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
snxxwyy
snxxwyy4d ago
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.
ἔρως
ἔρως4d ago
the thing is ... there isnt a lot to learn it is just confusing that is all
let x = async () => 5;
let y = await x();
let x = async () => 5;
let y = await x();
there, you used promises and await as long as you use this inside an async function, the y will be 5
snxxwyy
snxxwyy4d ago
Ohh I see, thank you
theoriginalandrew
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 responses
ἔρως
ἔρως4d ago
that is an easy way to think about it also, promises can take 1 cpu cycle or 1000 million years
theoriginalandrew
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 on
ἔρως
ἔρως4d ago
you have to ... await (if you want to go the easy route)
theoriginalandrew
😉
snxxwyy
snxxwyy4d ago
oh yeah that's a useful way to think about it, thank you
theoriginalandrew
no prob 🙂
Want results from more Discord servers?
Add your server