bhylu.com

Advertisements

Async Callbacks

A callback-based API needs a clear success and failure contract. Keep timer callbacks small and handle their errors where they run.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

function later(callback) {
  setTimeout(() => callback("Ready"), 20);
}
later(message => console.log(message));

What happens

The callback prints Ready after the timer becomes eligible.