BeginnerCoder

Lesson 15 — Async callbacks

Error-first callbacks

Before promises, Node used callbacks: you pass a function that is called when the work is done. The Node convention is error-first: cb(err, data). If something failed, err is an Error; otherwise it is null and data holds the result.

double(5, (err, res) => { if (err) return console.log(err.message); console.log(res); });

Why it matters: many Node libraries still use this style, and checking err first is how you avoid using bad data.

Want it another way?
Task

Make double() call cb(new Error("not a number")) when n is not a number, else cb(null, n * 2).

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.