Skip to curriculum
Jump to the editor

13.10. A Promise: finally that keeps the result

Hard
Topics

This question is in

13. Promise from scratch
30–50 min

A 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

JAVASCRIPT
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

  1. Everything from problems 13.7 to 13.9 still holds.
  2. finally returns a new MyPromise.
  3. onFinally runs once the promise settles, whether it fulfilled or rejected: once, as a microtask (like a then handler), with no arguments and this undefined.
  4. The new promise gets the same outcome: the same value, or the same reason. What onFinally returns does not change it, and a rejection is not recovered.
  5. If onFinally throws, the new promise rejects with that error, instead of the original outcome.
  6. If onFinally returns a MyPromise, the new promise waits for it. If it fulfills, the original outcome passes on (not its value); if it rejects, its reason wins.
  7. A missing or non-function onFinally passes the outcome on.
  8. finally starts by calling this.then with two functions, so it also works on any object with a then method.
  9. Do not use the built-in Promise inside MyPromise.

Examples

JAVASCRIPT
MyPromise.resolve("data")
  .finally(() => console.log("hide spinner"))
  .then((value) => console.log(value));
// hide spinner
// data
JAVASCRIPT
MyPromise.reject(new Error("offline"))
  .finally(() => "ignored")
  .catch((error) => console.log(error.message)); // "offline": not recovered
JAVASCRIPT
MyPromise.resolve(1)
  .finally(() => { throw new Error("cleanup failed"); })
  .catch((error) => console.log(error.message)); // "cleanup failed"
JAVASCRIPT
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 queueMicrotask for handler timing; do not use the built-in Promise inside MyPromise.
  • Waiting for a returned thenable that is not a MyPromise, and following promises passed to resolve or returned from then handlers, is problem 13.11. Here only a MyPromise returned by onFinally is waited for.
  • The exact number of microtasks before the new promise settles is not tested.

Hints

  1. finally must pass on a value or a reason without knowing in advance which. What do the two handlers you give then each need to remember?
  2. When onFinally returns a MyPromise, 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
Back to the question
Loading the editor…