Skip to curriculum
Jump to the editor

13.6. Promise.withResolvers and Promise.try

Easy
Topics

This question is in

13. Promise from scratch
10–20 min

Two small helpers fix two everyday annoyances with promises. The first: sometimes the code that settles a promise is not where the promise is made, like a queue that answers a request when a message arrives later. You end up writing let resolve; new Promise((r) => { resolve = r; }) again and again. Promise.withResolvers() gives you the promise and its two settle functions in one object. The second: a function that "returns a promise" can still throw before it gets that far, and then a caller's .catch never sees the error. Promise.try(fn) calls fn and turns everything it does, value, promise or throw, into one promise.

Write both in with-resolvers-try.js, as withResolvers() and promiseTry(fn, ...args).

Signature

JAVASCRIPT
function withResolvers()          // returns { promise, resolve, reject }
function promiseTry(fn, ...args)  // returns a Promise
  • withResolvers takes no arguments. promise is a new pending Promise; resolve and reject settle it.
  • promiseTry calls fn with the arguments that follow it and returns a Promise for the outcome.

Requirements

  1. withResolvers returns a plain object with exactly the keys promise, resolve and reject, in that order.
  2. promise is a real Promise, pending until one of the functions is called.
  3. resolve(value) fulfills it and reject(reason) rejects it, at any later time, called as plain functions. Only the first call counts.
  4. resolve given a promise or a thenable follows it, as the Promise constructor's resolve does.
  5. Every call to withResolvers makes a new promise and new functions.
  6. promiseTry calls fn straight away, during the call, once, with the extra arguments and with this undefined.
  7. A value returned by fn fulfills the promise; a returned promise or thenable is followed.
  8. An error thrown by fn rejects the promise; promiseTry itself never throws, even when fn is not a function (it rejects with a TypeError).
  9. Neither calls Promise.withResolvers or Promise.try.

Examples

JAVASCRIPT
const { promise, resolve } = withResolvers();
setTimeout(() => resolve("ready"), 100);
promise.then(console.log); // after 100 ms: "ready"
JAVASCRIPT
promiseTry((a, b) => a + b, 2, 3).then(console.log); // 5
JAVASCRIPT
promiseTry(() => { throw new Error("bad input"); })
  .catch((error) => console.log(error.message)); // "bad input"; nothing escaped the call
JAVASCRIPT
promiseTry(() => Promise.reject("not found"))
  .catch((reason) => console.log(reason)); // "not found": a returned promise is followed

Environment

  • Plain JavaScript in a browser page; the tests call the globals withResolvers and promiseTry.
  • You may use new Promise. You may not call Promise.withResolvers or Promise.try: one test replaces them for a moment to check.
  • Calling them with a different this (a subclass) is out of scope.

Hints

  1. The executor of new Promise runs synchronously. What can it do with the two functions it receives so that code outside can use them?
  2. Where must fn be called so that its exception becomes a rejection?

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…