13.10. A Promise: finally that keeps the result
Topics
This question is in
13. Promise from scratchA button shows a spinner while a request runs. Whether the request succeeds or fails, the spinner must go away, and the code after it must still see the real result: the data, or the error. Putting hideSpinner() in both then and catch works but repeats itself, and a catch that hides the spinner would also swallow the error. finally is the cleanup step that does neither: its callback runs on either outcome, gets no arguments, and the result passes through it unchanged.
This is the fourth of six problems that build a promise class. Write MyPromise in my-promise.js with everything from problems 13.7 to 13.9 (constructor, then, catch, MyPromise.resolve, MyPromise.reject) and add finally.
Signature
class MyPromise {
constructor(executor) // as in problem 13.7
then(onFulfilled, onRejected) // as in problem 13.8
catch(onRejected) // as in problem 13.9
finally(onFinally) // returns a new MyPromise
static resolve(value) // as in problem 13.9
static reject(reason) // as in problem 13.9
}Requirements
- Everything from problems 13.7 to 13.9 still holds.
finallyreturns a newMyPromise.onFinallyruns once the promise settles, whether it fulfilled or rejected: once, as a microtask (like athenhandler), with no arguments andthisundefined.- The new promise gets the same outcome: the same value, or the same reason. What
onFinallyreturns does not change it, and a rejection is not recovered. - If
onFinallythrows, the new promise rejects with that error, instead of the original outcome. - If
onFinallyreturns aMyPromise, the new promise waits for it. If it fulfills, the original outcome passes on (not its value); if it rejects, its reason wins. - A missing or non-function
onFinallypasses the outcome on. finallystarts by callingthis.thenwith two functions, so it also works on any object with athenmethod.- Do not use the built-in
PromiseinsideMyPromise.
Examples
MyPromise.resolve("data")
.finally(() => console.log("hide spinner"))
.then((value) => console.log(value));
// hide spinner
// dataMyPromise.reject(new Error("offline"))
.finally(() => "ignored")
.catch((error) => console.log(error.message)); // "offline": not recoveredMyPromise.resolve(1)
.finally(() => { throw new Error("cleanup failed"); })
.catch((error) => console.log(error.message)); // "cleanup failed"const saved = new MyPromise((resolve) => setTimeout(resolve, 100));
MyPromise.resolve("result")
.finally(() => saved) // waits 100 ms for saved
.then(console.log); // then "result"Environment
- Plain JavaScript in a browser page; the tests use the global class
MyPromise. - Use
queueMicrotaskfor handler timing; do not use the built-inPromiseinsideMyPromise. - Waiting for a returned thenable that is not a
MyPromise, and following promises passed toresolveor returned fromthenhandlers, is problem 13.11. Here only aMyPromisereturned byonFinallyis waited for. - The exact number of microtasks before the new promise settles is not tested.
Hints
finallymust pass on a value or a reason without knowing in advance which. What do the two handlers you givetheneach need to remember?- When
onFinallyreturns aMyPromise, what should happen when it fulfills, and when it rejects?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it