2 ms·
This is probably not the answer you're looking for, but you could look into ways that make callbacks enjoyable. The syntactic sugar provided by CoffeeScript goe
by athenot 10y ago
This is probably not the answer you're looking for, but you could look into ways that make callbacks enjoyable. The syntactic sugar provided by CoffeeScript goes a long way to make readable (and, dare I say, elegant) what would be a tangled mess in straight up JavaScript:
# Get profiles LIST as JSON
.post "/list", (req, res) ->
db.maria.query getProfilesQuery, { userId: req.session.passport.user }, false
.on "result", (result) ->
rows = []
res.status(200)
result
.on "row", (row) -> rows.push row
.on "error", (error) -> logger.warn error
.on "end", ->
res.json rows
res.end()
5 callbacks here but they don't impede readability.
- Mahn 10y agoMaybe it's just me, but this would look infinitely more readable to me if I could tell where each callback starts and where it ends, i.e. in plain JavaScript.
- always_good 10y agoWhat impedes readability here is that you don't simply have an async function `getProfiles({ userId })` that gives you a list of profiles, either taking a callback or returning a promise. A lot of that is pointless noise. You don't need to set the status code to 200 or call end(). `res.json(profiles)` will do. And these days, Coffeescript is just Javascript with some optional parens. Can't think of many upsides in 2017 to not just using ES6.