Now we also learn about promises,
often an alternative to working with callback functions,
which you pass directly to the method
that starts the async operation.
Now in both scenarios, things can go wrong.
Errors could occur.
And since async operations
work such that an operation is started
and the code thereafter executes
without waiting for that result,
you can't use try catch here around your promise.
So we can't do that here.
I mean, we can write this code,
but it won't work as intended.
And we also can't do that if we use a callback function.
Wrapping this with try catch also would not work as intended
because try would only check if calling read file succeeds
and not whether the result of read file is a success.
That's why in case of the callback approach,
we get this error parameter,
which tells us whether an error occurred or not.
And then we can always use a if check
to see if error is set, if it's truthy as we learned,
and then do something with it.
That's something we can do to handle errors
when working with callbacks.
And I'll leave it at that dummy example here
because we'll see better, more concrete examples
in the later course sections.
In case of promises, it's a bit different.
Here in this anonymous function,
we only get the data if the operation here
reading the file succeeded.
If that operation failed,
we instead can add another method here
to this promise chain and that's the catch method.
Promises have a then method to handle the success case
and catch to handle any errors
that might've occurred in a previous promise.
Catch also takes a function,
but a function which gets that error object
that describes the error that occurred.
And then here we can console.log that error
or handle it in any way we wanna handle it.
And that's how you do handle errors
with promises or callbacks.
Again, we'll see better a little bit more concrete examples
a little bit later in the course,
but that's already the knowledge you need
to understand how error handling will work later on.