Can we achieve the same goal from callbacks & promises, if so then why use one over the other?
-
2Possible duplicate: http://stackoverflow.com/questions/22539815/arent-promises-just-callbacks – IronAces Aug 31 '16 at 13:56
-
Promises are where asynchronous programming meets functional programming. I believe it's the most important part of ES6. – Redu Aug 31 '16 at 14:50
-
This is an Opinion Based Question – JΛYDΞV May 22 '22 at 02:59
-
When should I use one over the other, or what advantage is there for using one over the other. Those have been asked though. – JΛYDΞV May 22 '22 at 03:06
2 Answers
Besides the reasons you mentioned, you can store the promise and use it elsewhere in your code. Promises also follow a more object oriented paradigm.

- 1,986
- 5
- 29
- 37
(Not opinion-based:)
Propagating errors correctly back to a call-site with plain callbacks is near-impossible in a long chain of asynchronous operations, without reinventing something like promises.
If you throw
in a regular callback, where does the error go? Compare:
setTimeout(() => { throw new Error("Fail"); }, 1000);
vs.
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
wait(1000)
.then(() => { throw new Error("Fail"); })
.catch(e => console.log("Caught: " + e)); // Caught: Error: Fail
In the former case it goes straight to the browser's console without the JS knowing, whereas in the latter case it's caught by the JS, which can take corrective action.
Propagating errors correctly in general without promises would require try {} catch() {}
with propagation magic around every callback in the code, something I've never seen.

- 40,579
- 17
- 100
- 158
-
Your examples are not comparable. The first example should be: ```var wait = setTimeout; wait(1000, function() { try { throw new Error("Fail"); } catch (e) { console.log("Caught: " + e); } })```. As you can see, this example is significantly more straightforward and more understandable than the example with a promise. – Mihail H. May 28 '21 at 11:04
-
@MihailH. Sure, but you haven't propagated the error anywhere. [The point of promises is to give us back functional composition and error bubbling in the async world](https://gist.github.com/domenic/3889970#what-is-the-point-of-promises). For readability, use `async`/`await`. – jib May 28 '21 at 20:08