Async Await try-catch hell

preview_player
Показать описание
Async Await is heaven... until error handling comes into play. Learn how to avoid try/catch hell when writing async JavaScript code.

#shorts #js #programming
Рекомендации по теме
Комментарии
Автор

Protip: Avoid the pyramid of doom by never indenting your code 🤙.

gabriel.quagliano
Автор

"a sink"
"a weight"

k

lvenir
Автор

Basically you made Javascript looks like Golang Lol.

tdubab
Автор

Congrats! You one step closer to using monads for error handling

alexeyl
Автор

My life is pretty dull and uneventful, so this was the best 45 seconds of my life.

wumbl
Автор

I've been asynchronously awaiting for this and finally this video showed up

arwahsapi
Автор

This reminds me of a quote:
"Errors are values"

erbesharat
Автор

rust developers can just put ? in front of the function. If an error occurs, the function halts and returns the error. It's awesome for those that understand it, a nightmare for someone unfamiliar with rust lol.

rumplstiltztinkerstein
Автор

Holy shit, most youtubers would have turned this into a 10 minute monetized video. You are amazing.

bensas
Автор

And then you will end up with tower of ifs, when each function depends on previous results
You've just writed try catch using if statements nothing changed
What you can do is catch them all together
And make specific errors for each function and then switch through errors in catch block if you need to perform something for each error case

Ipauler
Автор

Tips:
- Make the function take a promise as a parameter
- use "catch" method of the promise
- Call this function "to"

function to (promise) {
return promise.then(data => [null, data]).catch(err => [err])
}

const [err, data] = await to(fetch(/*thatUrl*/))


Notes: Dont need to have an async function or to use try-catch; its reusable for any function returning a promise; its a oneliner in a oneliner \o/

Vaidd
Автор

You still have to if (data !== null) every time if you want your program to have any decent behaviour though.

cat-.-
Автор

To use the "awesome" function in typescript, you can do it like this:

async function awesome <T>(
prom: Promise<T>
): Promise<[T | null, any]> {
try {
return [await prom, null];
} catch (error) {
return [null, error];
}
}

omararmandourquidezcastro
Автор

Such a quality of life improvement. I read this in an article once, when I was just starting out, and started writing it, just waiting for the other shoe to drop (I had to defend it before my boss, who was skeptical at first, even though he hated try catch and longed for the async.js days); cause why wasn't everyone doing this? No regrets!

beorntwit
Автор

Swift just got Async/ Await feature and now I'm getting this as recommended.

TheHyxD
Автор

So from tower of doom with try/catches to tower of doom with if/elses?

monkeyinadrawer
Автор

So imagine when we realise that we could be adopting a functional paradigm and we can live inside of a monad

ShreksSpliff
Автор

Return error as value is amazing. I love Rust.

oglothenerd
Автор

I'm starting to love programming. I enjoy it now. I understand it now.

catherineepps
Автор

Nice tips! This applies to Golang as well and it's error handling nightmare

jordinario