Skip to content

Chapter 20 of 33

Callbacks

Design callback contracts, higher-order functions, error-first APIs, and control flow.

34 minutes 10 quick checksBy Subha Prasad
Lesson 20 of 33Course navigation

Lesson content

Read, practise, then check your understanding

A callback is passed for later invocation. Array methods use synchronous callbacks; timers, events, and I/O often invoke asynchronously.

Contract clarity

function loadUser(id, callback) {
  readRecord(id, (error, record) => {
    if (error) return callback(error);
    callback(null, normalize(record));
  });
}

Node-style callbacks conventionally receive error first and must be called exactly once. Document timing, arguments, receiver, cancellation, and error propagation. Deeply nested callbacks obscure sequencing and cleanup; extract named functions or adapt to Promises. Callbacks remain ideal for events, local predicates, and APIs with repeated results.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes callback?
Which JavaScript term matches this description: A function supplied for invocation by another operation.
Which statement best describes error-first callback?
Which JavaScript term matches this description: A Node convention where the first argument represents failure.
Which statement best describes higher-order function?
Which JavaScript term matches this description: A function receiving or returning functions.
Which statement best describes callback queue?
Which JavaScript term matches this description: A host-managed queue whose tasks run after the call stack is available.
Which statement best describes inversion of control?
Which JavaScript term matches this description: Delegating when and how callback code is invoked to another component.

0 of 10 checks passed

Your progress is saved on this device.

Callbacks | JavaScript Lesson | Subha Prasad