In Exercise: Wrap and Chain the Kiosk Lookup, you looked up a family member’s pick and then its gallery with a chain of .then() calls and one .catch() at the end. The chain works, but you read it from handler to handler, and each handler sees only the value handed to it.
In this lesson, you’ll rewrite that lookup as an async function, which reads from top to bottom like the code you wrote before promises. You’ll handle a failed lookup with try and catch and plan each of Tolu’s picks in turn with a loop. Start with the chain you already have and the same steps written with async and await.
Write the Chain as an async Function
The chain spreads one job, finding where a pick hangs, over several handlers. Here’s the chain from the exercise again, inside a function that logs the title and the gallery:
function planPick(id) {
findArtwork(id)
.then((artwork) => {
console.log(`Title: ${artwork.title}`);
return findGallery(artwork.galleryId);
})
.then((gallery) => console.log(`Gallery: ${gallery}`))
.catch((error) => {
console.log(`Couldn't plan the visit: ${error.message}`);
});
}
You’ll work in your museum folder with the files from the earlier lessons. Your find_artwork.mjs wraps the old kiosk’s lookupArtwork(), and kiosk.mjs still has the stand-in’s findGallery(). If you need fresh copies, they’re here:
The family’s picks:
picks.txt
Tolu: 28560, 111628
Grandma Bisi: 27992
Folake: 6565, 20684
Tunde: 16568
The old kiosk, which answers through a callback:
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);
}
Your wrapper, which returns a promise of the kiosk’s answer:
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 stand-in, whose findGallery() you’ll keep using:
kiosk.mjs
import { setTimeout as delay } from "node:timers/promises";
const artworks = {
28560: { title: "The Bedroom", galleryId: 241 },
111628: { title: "Nighthawks", galleryId: 262 },
27992: {
title: "A Sunday on La Grande Jatte — 1884",
galleryId: 240,
},
};
const galleries = {
240: "Gallery 240, second floor",
241: "Gallery 241, second floor",
262: "Gallery 262, second floor",
};
export function findArtwork(id) {
const artwork = artworks[id];
if (artwork === undefined) {
return Promise.reject(new Error(`Artwork ${id} isn't on view`));
}
return delay(300, artwork);
}
export function findGallery(galleryId) {
return delay(200, galleries[galleryId]);
}
Two keywords let you write the same steps in one column. Putting async in front of function makes an async function, which always returns a promise. Inside it, await in front of a promise pauses the function until that promise settles. If it’s fulfilled, then await gives you its value, so you can store it in a variable.
Save this version as find_place.mjs. It returns a line about the pick instead of logging it, so you can see what the caller gets:
find_place.mjs
import { findArtwork } from "./find_artwork.mjs";
import { findGallery } from "./kiosk.mjs";
async function findPlace(id) {
const artwork = await findArtwork(id);
const gallery = await findGallery(artwork.galleryId);
return `${artwork.title}: ${gallery}`;
}
const place = findPlace("28560");
console.log(place);
console.log(await place);
Run it, and the first line shows a promise you’ve seen before:
$ node find_place.mjs
Promise { <pending> }
The Bedroom: Gallery 241, second floor