Skip to main content

Side by side

CallbackvsPromise

What is the difference between a callback and a promise?

Updated 2 min read6 differences

In short

A callback is a function you hand to another function to run when it's done, while a promise stands for a future result you can chain and catch errors on.

Callback

A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.

Read the page on Callback

Promise

A promise is an object that represents the eventual result of an asynchronous operation, letting code react to success or failure once the work completes.

Read the page on Promise

Callback and Promise compared

AspectCallbackPromise
What it isA function passed in and called laterAn object that stands for a future value
Steps in orderNested inside each otherChained with .then() or written with await
ErrorsChecked in every callbackOne .catch() or try/catch for the whole chain
How often it runsAs many times as the caller decidesSettles exactly once
Work in parallelCounting finished tasks by handPromise.all, Promise.race and similar helpers
Typical useEvents, timers, array methods, older Node.js APIsNetwork requests, file reads and other one-time results

The difference, explained

Both are ways to deal with work that finishes later, such as reading a file or calling an API. With a callback you hand a function to the operation, and the operation calls it with the result, in Node.js often as (error, result). With a promise the operation hands something back instead: an object that is pending at first and later either fulfilled with a value or rejected with an error.

The difference shows when steps depend on each other. With callbacks, each step goes inside the previous step's callback, so the code drifts to the right and errors have to be checked at every level, a pattern known as callback hell. Promises flatten this into a chain of .then() calls, send any error to a single .catch(), and combine with helpers like Promise.all to run work in parallel.

Promises build on callbacks rather than replacing the idea: .then() still takes a function. What they add are guarantees. A promise settles only once and its handlers always run asynchronously, which prevents bugs where a callback fires twice or too early. async and await, now the most common style, are syntax on top of promises.

A common misconception is that callbacks are outdated. Event listeners, array methods like map and many APIs rightly use callbacks, because they run many times or straight away. Promises fit one-time asynchronous results, and older callback-style APIs can be wrapped, for example with Node.js's util.promisify.

Which one should you use?

Choose Callback when…

  • The function runs many times, as with event listeners or array methods.
  • You work with an older API that only offers callbacks.
  • The work is synchronous and you only need to customize one step.

Choose Promise when…

  • You are waiting for one result, such as a network request.
  • Several asynchronous steps depend on each other.
  • You want one place to handle errors, or to run tasks in parallel.

Reading a file, then parsing it

Callbackjavascript
import fs from "node:fs";

fs.readFile("config.json", "utf8", (err, text) => {
  if (err) return console.error(err);
  try {
    const config = JSON.parse(text);
    console.log(config.port);
  } catch (parseError) {
    console.error(parseError);
  }
});
Promisejavascript
import { readFile } from "node:fs/promises";

readFile("config.json", "utf8")
  .then((text) => JSON.parse(text))
  .then((config) => console.log(config.port))
  .catch((err) => console.error(err)); // one place for every error

Readers ask

Is async/await the same as promises?

Underneath, yes. An async function always returns a promise, and await pauses that function until a promise settles, so it is a more readable way to write promise chains.

What is callback hell?

Code in which each asynchronous step is nested inside the previous step's callback, so it becomes deeply indented and hard to follow, with the same error checks repeated at every level.

Can I turn a callback API into a promise?

Yes. Wrap it in new Promise((resolve, reject) => …), or in Node.js use util.promisify for functions that take an (error, result) callback.

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings