One of the basic techniques required for building things in JavaScript, is the ability to effortlessly convert between functions that use callbacks and functions that return promises.

Callback ➑ Promise

Promise ➑ Callback

Bonus: NodeJS util.promisify and util.callbackify

When converting between callback style and promises, NodeJS has a handy pair of tools specifically made for this purpose. The one thing to note is that the callback function signature is (error, data)

Which one is best?

Whichever makes your code more intelligible.

Each is a separate way of organizing the paths that your code takes. Callbacks are slightly faster and with less overhead, but the performance difference is negligible compared to the gains in readability of your code. Naturally, performance may be important in highly repetitive tasks, but coincidentally, these tasks tend to be more readable when written in the callback form.

My goal is to serve humanity and to bring happiness to others. I want to understand the problems around us and help find solutions. https://www.bayanbennett.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store