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.
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.
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.
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.