DEV Community

Discussion on: Error Handling in JavaScript (Golang Style)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I would wrap it in a higher order function though, something that takes an async function and returns a new async function with the added catch logic

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Something like this, I guess:

const safe = fn => (*args) => fn(*args)
   .then(res => [res, null])
   .catch(err => [null, err])
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bibekkakati profile image
Bibek

Yeah. That can be way of implementing it.