Skip to curriculum
Jump to the editor

12.4. Promisify with several results and a custom marker

Medium
Topics
20–30 min

A plain promisify turns (error, value) callbacks into promises, but real callback APIs are not all that simple. Some pass several values: a command runner calls back with (error, stdout, stderr), and a DNS lookup with (error, address, family). A promise has only one value, so those must be packed into one object. And some functions already have a better promise version written by hand, which promisify should hand out instead of building a wrapper. Node.js handles that with a shared marker, the symbol Symbol.for("nodejs.util.promisify.custom"): a function stores its own promise version under it.

Write promisify(fn, names) in promisify.js. The file already sets promisify.custom to that symbol.

Signature

JAVASCRIPT
function promisify(fn, names) // returns a function that returns a Promise
promisify.custom              // Symbol.for("nodejs.util.promisify.custom")
  • fn: a function whose last argument is a callback (error, ...values).
  • names: optional array of strings, one name per callback value.
  • An error is a truthy first argument.

Requirements

  1. Without names, the returned function's promise fulfills with the callback's first value.
  2. With names, it fulfills with a new object that has one key per name: names[i] holds the i-th value after the error. Each name is an own data field, "__proto__" included. A name with no value holds undefined; values beyond the names are dropped.
  3. A truthy error rejects the promise, with or without names.
  4. If fn[promisify.custom] is a function, promisify returns that function itself (not a wrapper), even when names are given. If it holds any other truthy value, promisify throws a TypeError.
  5. The function promisify returns has promisify.custom set to itself, as a non-enumerable property. So promisifying it again returns the same function.
  6. As in a plain promisify: arguments and this are passed to fn itself (even when it has its own call or apply property) with the callback last, a throw inside fn becomes a rejection, a second callback changes nothing, and a non-function fn throws a TypeError.

Examples

JAVASCRIPT
function run(command, callback) {
  setTimeout(() => callback(null, "built in 2s", "1 warning"), 100);
}

const runAsync = promisify(run, ["stdout", "stderr"]);
await runAsync("build"); // { stdout: "built in 2s", stderr: "1 warning" }
JAVASCRIPT
function lookup(host, callback) { callback(null, "203.0.113.7"); }

await promisify(lookup, ["address", "family"])("example.test");
// { address: "203.0.113.7", family: undefined }
JAVASCRIPT
function sleep(ms, callback) { setTimeout(callback, ms); }
sleep[promisify.custom] = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

promisify(sleep) === sleep[promisify.custom]; // true: the hand-written version
JAVASCRIPT
const once = promisify(run);
promisify(once) === once; // true

Environment

  • Plain JavaScript in a browser page; the tests call the global promisify from promisify.js.
  • The tests use a virtual clock for setTimeout, so no test waits for real time.
  • Copying fn's other properties or prototype onto the new function is out of scope.

Hints

  1. How do you collect every callback value after the error, however many there are?
  2. What can promisify check first, before it builds anything, to return an existing promise version?
  3. If the new function stored itself under the marker, what would a second promisify call find?

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…