Hi! Thanks for explaining how throw is not type safe. That was eye-opening!
The makeHttpRequest function on the example is async. That returns a Promise.
Why not simply reject the promise instead?
Glad you enjoyed the article!
Well, using Promise.reject inside an async function is just an alternative to using throw.
Consider this example:
const doSomething = async () => Promise.reject(new Error('oh nooo'))
If you await on this function above, you will have to wrap it inside of a try / catch statement to prevent a runtime error. Further, there's no way you could encode the potential for failure into the above function. It's identical in behaviour to using throw ... So you lose all type safety.
try / catch
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.