DEV Community

Discussion on: What is your opinion on callbacks in JavaScript?

Collapse
 
avalander profile image
Avalander

I always transform callback based APIs to use promises for convenience. That being said, people talk as if we were stuck in the stone age before promises but that's not true. A promise is basically a convenience structure to handle callbacks, and it is/was possible to write similar structures to handle callback APIs back in the days. It's just that most developers didn't bother because ¯\_(ツ)_/¯.

Regarding ...Sync functions, it depends. If I am writing a script to migrate a database, for instance, or doing some initialization when starting up a server, and the script/app can't do anything else until that operation is complete, using the ...Sync function makes the code slightly less convoluted and I prefer it. On the other hand, if the operation is used in an HTTP request handler, I want the app to be free to do other stuff while waiting for the operation, so I go for the asynchronous version.