Arpit Parmar
Arpit Parmar

Reputation: 300

Why express and node have heavy usage of callbacks when promises are the recommended choice?

I am given a task to complete using Node, express and MySQL. Now express requires usage of callbacks but for MySQL I want to use Sequelize ORM which is based on promises.

What should I do in this situation?

Should I go ahead and use Sequelize Promises with node and express's Callbacks (which I have read is not a good practice!) or do you know something which would be a better choice in this situation.

I also want to know why Node and express uses Callbacks instead of Promises? Is it intentional or something else?

Thank you for the help in advance!

Upvotes: 1

Views: 52

Answers (1)

Ran Halprin
Ran Halprin

Reputation: 527

Express and node were created before promises were standardized (which is not that long ago).

There is no problem in using promises inside express callbacks, you can use this pattern to define your routes neatly:

app.use('/myroute', require('./myroutehandler'))

and in myroutehandler:

module.exports = function (req, res, next) {
    ...
}

What you want to avoid is mixing promises and callbacks in your code, because it'll be hard to maintain. To avoid that when using libraries that are callback-based you can use something like es6-promisify.

Upvotes: 3

Related Questions