BeginnerCoder

Lesson 23 — Promisify a callback

Wrap old callback APIs

Older code reports results through a callback — a function you pass in, called as cb(error, value) when the work finishes (error-first style). Promisifying wraps such a function so it returns a Promise instead: reject when there is an error, resolve with the value otherwise. Node even ships util.promisify for this.

function readLater(cb) { setTimeout(() => cb(null, "data"), 10); }
// promisified: readLaterP().then(console.log)

Why it matters: it lets you use await with legacy libraries instead of nesting callbacks.

Want it another way?
Task

Finish promisify so the failing call prints error: boom and the working call prints 42.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with JavaScript Intermediate

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.