So now that we saw how we resolve a promise and handle this value in the then callback, let's reject it.
So for this, I'll comment out resolve here and instead I'll call reject and reject here, I will also pass
a value, this time maybe a Javascript object just to mix it up, where I have an error code maybe, let's
say 500 and a message, an error occurred
and you're totally free to pass back what you want,
you could still just pass a string if you want to do that.
So now we call reject, if we don't change anything else let's save this and let's reload this in the browser
and let's see what happens.
We still execute this right after set timeout but then we get an error that we didn't catch this rejected
value basically. We don't get the console log from before,
so we don't execute the then blocks here or actually we do but we only handle success cases here in the
first argument you passed to then. We can also pass a second argument though, you could pass another function
here which will then handle any rejected promise results, so any errors,
of course this argument name is also up to you.
There you could console log err.code and err.messages
and I know that I can access both properties because I pass them here in this object.
If we do this and save and we reload our application again, we see this is executed right after set timeout
again and then after three seconds, we get this log with the error code and our error messages. We
no longer get this error result from before.
We also get undefined,
that's stemming from the second then call where we just don't get the argument because in the error
case, we don't return a value we could process here.
Now this is one way of handling promise rejections,
the more common way is to not use this second argument though. I will quickly comment this out to keep
it for reference but what I want to show here is that you typically wouldn't use that second argument
but to make this super easy to read,
you can simply add catch here,
so I'll add another method to this chain here, catch, I could add it in a new line but let me stick it right
to the end of that line
and here I can now also pass a function which will only be executed in rejected cases,
so never on resolve and there I can also console log err.code and err.messages.
The cool thing about catch is it will catch any errors occurring at any step prior to catch,
so either on the first then block or the second one
and I could also add catch here right after the first then block and then it would only react to errors
which were thrown at this step and would ignore errors at the second step,
so that's possible too. Here I'm now catching errors occurring at any point of time in this
promise chain
and if we now save this and reload this application again, we again see executed right after set timeout
and then we get 500 and an error occured,
you see that the undefined log is missing because catch caught the error immediately and prevented
the promise from executing any other lines because we added it at the end here.
And this is how we can work with promises
in its very basic feature set, then and catch are the two important methods you have to be aware. Use
then to use the normal return values in cases where no error occurred and use catch to handle any
errors which might occur.
Now with that knowledge, let's go back to the promises we already used in this application and try to
understand those a bit better.