At the end of Follow a Failure Down the Chain, a final .catch() gave Tunde a clear message when his pick wasn’t on view. That chain ran on a stand-in, though. The findArtwork() in kiosk.mjs only pretends to be the kiosk, with delay() and a promise. The museum’s old kiosk has a single helper, lookupArtwork(id, callback), and it takes a callback, so it can’t join a chain.
In this lesson, you’ll wrap that helper in a function that returns a promise, so you can chain it like any other step. You’ll also see exactly when your wrapping code runs and when you shouldn’t make a promise yourself at all. Start with the old kiosk and the shape of the answer it gives.
Wrap the Kiosk’s Lookup
A chain needs a promise, but a callback-based helper hands back nothing. You need code that turns its callback’s answer into a promise’s outcome. First, save this toy stand-in for the old kiosk as old_kiosk.mjs in your museum folder:
old_kiosk.mjs
const artworks = {
28560: { title: "The Bedroom", galleryId: 241 },
111628: { title: "Nighthawks", galleryId: 262 },
27992: {
title: "A Sunday on La Grande Jatte — 1884",
galleryId: 240,
},
};
export function lookupArtwork(id, callback) {
setTimeout(() => {
const artwork = artworks[id];
if (artwork === undefined) {
callback(new Error(`Artwork ${id} isn't on view`));
return;
}
callback(null, artwork);
}, 300);
}
The kiosk is slow, so lookupArtwork() waits 300 milliseconds with setTimeout() before it answers. Its callback is error-first, like the callback of readFile() in From Callbacks to Promises. For an unknown ID, like Tunde’s 16568, the first argument is an error. Otherwise, it’s null, and the artwork comes second.
To make a promise of your own, you call the Promise constructor with new Promise(). You pass it a function, called the executor, which JavaScript calls with two functions of its own, usually named resolve and reject.
Calling resolve(value) with a plain value, like an artwork object, fulfills the new promise with value, and calling reject(reason) rejects it with reason. If value is itself a promise, then the new promise follows that promise’s outcome instead.
Save this wrapper as find_artwork.mjs next to the old kiosk:
find_artwork.mjs
import { lookupArtwork } from "./old_kiosk.mjs";
export function findArtwork(id) {
return new Promise((resolve, reject) => {
lookupArtwork(id, (error, artwork) => {
if (error) {
reject(error);
return;
}
resolve(artwork);
});
});
}
The executor starts the lookup and hands the kiosk a callback. When the kiosk answers, that callback settles the promise. The error path calls reject(error), and the return after it stops the callback there. The success path calls resolve(artwork). So every path through the callback calls exactly one of the two. A path that called neither would leave the promise pending, and no handler would ever run.
To try the wrapper on a pick that’s on view and one that isn’t, save this script as try_find.mjs:
try_find.mjs
import { findArtwork } from "./find_artwork.mjs";
function report(id) {
findArtwork(id)
.then((artwork) => console.log(`${id}: ${artwork.title}`))
.catch((error) => console.log(`${id}: ${error.message}`));
}
report("28560");
report("16568");
Run it, and each lookup ends in the handler that matches its outcome: