Skip to content
BENIBIX Learn

Callback vs Promise

A callback is a function you hand over to be run later. A promise is an object for a result to come, which you can chain with then. Promises avoid deep nests of callbacks.

Development🧭 Programming Paradigms

Callback Function

A function you give to other code. That code runs it later.

A callback is a function you hand to other code, so that code can run it later. You often pass one to a timer, a click handler or a download. When the time comes, the other code calls your function back. Callbacks were the first way to write async code in JavaScript. Many callbacks inside callbacks get hard to read, which is why promises came along.

Example
Emma gives setTimeout a callback and a wait of 2 seconds. Then her function runs and shows a message.
Why it matters
Callbacks are used for clicks, timers and file reads. List tools like map take one too.
Full entry
Development🧭 Programming Paradigms

Promise (JavaScript)

An object for a result not ready yet. It ends in a value or an error.

A promise is an object that stands for a result that is not ready yet. It starts as pending, then ends as fulfilled with a value or rejected with an error. You attach what to do next with then, and what to do on failure with catch. Promises can be chained, so steps read in order instead of nesting. Other languages call the same idea a future.

Example
Lucy's app calls fetch for a weather page. It gets a promise at once, and the forecast shows up when the promise is fulfilled.
Why it matters
Most modern JavaScript tools give back promises. async/await is built on top of them.
Full entry

In one line

Callback Function
A function you give to other code. That code runs it later.
Promise (JavaScript)
An object for a result not ready yet. It ends in a value or an error.