The initial state of an asynchronous promise is
pending, but we have a guarantee that it will settle. How do we tell the computer what should happen then? Promise objects come with an aptly named
.then() method. It allows us to say, “I have a promise, when it settles, then here’s what I want to happen…”
In the case of our dishwasher promise, the dishwasher will run then:
.then() is a higher-order function— it takes two callback functions as arguments. We refer to these callbacks as handlers. When the promise settles, the appropriate handler will be invoked with that settled value.
onFulfilled, is a success handler, and it should contain the logic for the promise resolving.
onRejected, is a failure handler, and it should contain the logic for the promise rejecting.
We can invoke
.then() with one, both, or neither handler! This allows for flexibility, but it can also make for tricky debugging. If the appropriate handler is not provided, instead of throwing an error,
.then() will just return a promise with the same settled value as the promise it was called on. One important feature of
.then() is that it always returns a promise. We’ll return to this in more detail in a later exercise and explore why it’s so important.