Yes, this is the right way to associate state with actions.
Operations with the .then chain are very common and are usually our building block when circled around. It is in the very core of promises.
What you do is correct and idiomatic.
For a curious spirit will show it.
To verify this, we can check the promises specification.
We want to verify that:
- He chains
- In case of rejection, it does not call the handler in the chain, and then
- He rejects the following promise returned from
then for the same reason. - Executed in a return value sequence.
Check them out in order using the specification - specifically .then :
1. He's a chain
7.1, then it is necessary to return the promise [3.3].
Great, let him verify that he is also chaining at fullfillment
If either onFulfilled or onRejected returns x, run the promise resolution procedure> [[Resolve]] (prom2, x).
Great, therefore, we know that when our promise allows or rejects, then our then handler is called with the appropriate parameter. So .then(do_A).then(do_B) will always work if do_A allows.
2. If rejected, it does not call the handler in the chain, and then
7.iv. If onRejected is not a function and promise1 is rejected, then promise2 must be rejected for the same reason.
Great, so it rejects and calls onRejected if it is there, if it does not catch.
3. He rejects the following promise returned from then for the same reason.
We just looked at it in 2.
4. It performs the return value in the transmitting sequence.
It again
If either onFulfilled or onRejected returns x, run the promise resolution procedure [[Resolve]] (prom2, x).
So, if you install onFulfilled, it will start the resolution process. The resolution process itself dictates:
The procedure for resolving promises is an abstract operation that takes as a contribution a promise and a meaning that we designate as [[Decide]] (promise, x). If x is obscured, he tries to promise by accepting state x, on the assumption that x behaves at least as a promise. Otherwise, it fulfills a promise with a value of x.
If / when you call solvePromise with a value of y, run [[Resolve]] (promise, y).
Where y is the return value of x.
Excellent! therefore it works.