Press "Enter" to skip to content

Error handling Promises in JavaScript – Fun Fun Function


good Monday morning today we are

exploring error handling I’m going to be

talking about how you shouldn’t swallow

errors by doing crash reporting inside

on functions instead we’re going to look

at how you can let errors propagate up

to the interface by jesus promises I am

mgj and you are watching funfun function

when I first started using promises I

tend to write things that look a little

bit like this okay so what does this

code do and what are problems with it I

will tell you but if you want you can

pause the video now and think a little

bit about it on your own first

okay so let’s walk through what delete

tap done so it calls a delete method

here on the database object that we get

from somewhere this is fictional code

deal with it as we see here ah delete

seems to be returning a promise using

the promise here are the code is going

to log to the console whether or not the

tap deletion succeeded what the code

does here is crash reporting what I mean

by that is that it will inform testers

and developers that something has gone

wrong but you know who will not be

notified the user

let me edit this code so that I fit

properly on screen here

I think move it code down at it too the

problem with this code is that if

something goes wrong here the user will

not actually be notified that the

calculation failed

if the delete cat operation fails there

is really no nice graceful way to the

sort for the software to to handle that

error the only thing I can think of is

to have some kind of weird mechanic

where the delete operations are

automatically retried or something but

that seems really weird so the more

realistic thing here is that we need

some way for this code delete tab to

notify it the higher level code the code

that is calling them in tab that the

operation has failed and this is

something that promises solve for you

very easily by the way if promises are

completely new to you you might want to

check out my video on promises here a

link to that is also in the episode

description back in the day when I was

writing code like this I was missing out

on one of the most useful aspects of

promises in this code here I am

basically just using promises as

glorified callbacks in this code I’m not

making use of the fantastic capability

of promises that is that they can be

returned and passed around let’s pause

for a bit and think about the concept of

a promise not the JavaScript concept but

the the real-life concept of a promise

I realize examples are I recently bought

an apartment when you do that at least

here in Sweden you go to the bank and

you get something called a loan promise

no the promise is actually its real name

if you do a direct translation from

Swedish basically you show up at the

bank you show them and prove to them how

much you earn that your credit check you

tell them what kind of apartment you are

shopping for in what area what price

range and the bank provides you with a

loan promise they promise you that given

these conditions they will lend you this

amount of money you don’t actually have

the money you don’t actually have a loan

but you have this promise normally

holding that promise you can kind of

start acting like you actually have the

loan you can go and bid on apartments

with this thing this promise promises in

JavaScript what sort of the same way but

before I talk about that I would like to

address the fact that this Friday the

patreon for wonderful function launched

and asked on recording we are over 800

patient ah that’s insane I expected it

to take months to reach this level and

we did it in 48 hours we are now in the

top 20 in the education category on

patreon

this Friday was so intense ah it was one

of the best days in my life in a very

long time so if you are patron thank you

so much if you are not a patron and you

are curious about the walks and the why

of becoming a patron for fun fun

functions you can go to

patreon.com/scishow

[Music]

or just click here where would I go yeah

promises in JavaScript they work sort of

like my real life loan promise when we

call database database doesn’t delete

here in this code it returns the promise

of a delete operation so it doesn’t

return a success or a failure it returns

the promise of success if you wanted a

courageous queue that has a watch my

video on monads you know that promises

or Moana you really don’t have to watch

that video or know about monads

in order on attendance but if this is

happens to be a very good example of

what monads conceptually are so promise

it’s not the success itself it’s the

promise is actually more like an

abstract concept that allows us to act

as if we had success it’s similar to how

I could use my loan promise to buy an

apartment I could go to people and say

given that my known promise resolves to

an actual loan I will buy this apartment

and they would say why are you speaking

like a robot but this video is not about

success it is about failure promises do

not always resolve to success they

sometimes are rejected with an error

perhaps my bank is big on semicolons and

they watch my youtube channel and they

go like screw you you standard jazz

hippie nothing likely but it could

happen

and this is why promises in JavaScript

can be rejected and that is very very

useful but only if you actually

return Providence and let them roam free

instead of trapping them inside your

functions like I’ve done here look now

what happens if I remove the handler yes

submitted and I return the promise

unless that get out of the function and

just imagine that we have a click

handler that’s all right so check out

this delete button click handler that

are your cemented always unique events

there as well so it grabs the cat ie

here from the fake elements the

officious ball element here and what is

then does is that Copley’s catch it gets

the promise because we are now getting

the promise here and here it will handle

the success and remove the item element

from the page using the cat ID and and

if we get an error to catch that and

it’s going to show a message message

message dialog where it says that item I

can this cap here swappigans probably

was was not delete it notice here how

the delete button click handler is in a

much better position to handle the error

than the previous example because this

live in the UI layer where we need to

present the user with with information

about the deletion failures and the

reason why we can pull this off now is

because the lead cap doesn’t swallow the

error from the data distillation inside

of itself it returns me the promise so

that it can propagate up to the to the

UI this is a very simplified example

this is just one layer in a real

software you would have multiple layers

of this with that just kept returning

promises but it’s really the exact same

principle so what I’m trying to say here

is

if you are not able to great totally

gracefully handle the error that happens

inside your function you should let that

error propagate up to the higher level

the calling calling code so that that

code can a deal deal with it if it if it

likes or you crash reporting if it’s at

the top level don’t do tragic reporting

inside of functions let promises

propagate up and let the top level code

deal with the crash reporting I want to

make a shout out because this episode

was kind of like the brainchild of my

ex-colleague philippe as x45 i’ve linked

his twitter handle in the episode

description you should follow him

because he has a lot of interesting

source like this you have just watched

an episode of fun fun function I

released leaves every Monday morning

Oh 800 DNC if you don’t want to wait

until next Monday morning you can check

out this episode right now that robots

are selected for your viewing pleasure

I am mvj until next Monday morning thank you

Please follow and like us: