DEV Community

Discussion on: Capture error and data in async-await without try-catch

Collapse
 
rhymes profile image
rhymes

Nice!

FYI: You might still need try..catch though, the wrapper catches asynchronous errors but if inside callApi anything you call issues a "standard" error that exception will bubble up to the user.

const callApi = async () => {
  const { error, data } = await wrapper(fetchData(2000, false));
  callAFunctionThatDividesByZero()
  if (!error) {
    console.info(data);
    return;
  }
  console.error(error);
}
Collapse
 
sadarshannaiynar profile image
Adarsh • Edited

Thanks! :) And Yeah on regular exceptions I would need a catch but I was focusing on the API part only! :)

Collapse
 
rhymes profile image
rhymes

I like it, it reminds me of how Go does error handling :D

Thread Thread
 
sadarshannaiynar profile image
Adarsh

I still haven't worked with Go yet 🙈 planning to start soon.