Basic Callbacks in JavaScript

From the Async JavaScript cheat sheet ยท Callbacks ยท verified Jul 2026

Basic Callbacks

Traditional pattern for handling asynchronous operations with callback functions

javascript
// Simple callback
function getData(callback) {
  setTimeout(() => {
    callback('Data loaded');
  }, 1000);
}

getData(result => console.log(result));

// Error-first callback (Node.js pattern)
function readFile(path, callback) {
  fs.readFile(path, (err, data) => {
    if (err) callback(err, null);
    else callback(null, data);
  });
}
๐Ÿ’ก Callbacks are functions passed as arguments to be executed later
โš ๏ธ Can lead to "callback hell" with deep nesting
๐Ÿ“Œ Error-first convention: first parameter is for errors
๐Ÿ”„ Still used in many Node.js APIs and older libraries

Continue with Async JavaScript

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full Async JavaScript cheat sheet