4 ms·
> if (err) return callback(err) > That line is haunting me in my dreams now. What happened to the DRY principle? iferr = (succfn, errfn) -> (err, a...) ->
by nadaviv 13y ago
> if (err) return callback(err)
> That line is haunting me in my dreams now. What happened to the DRY principle?
iferr = (succfn, errfn) -> (err, a...) ->
if err? then errfn err else succfn a...
show_user = (id, cb) ->
db.query ..., iferr cb, (user) ->
...
FTFY, no promises needed. (not to say that promises don't have other advantages, but that specific complaint is easily fixable with an higher-order function to abstract it away)
(JavaScript version: http://coffeescript.org/#try:%20%20iferr%20%3D%20(succfn%2C%20errfn)%20-%3E%20(err%2C%20a...)%20-%3E%0A%20%20%20%20if%20err%3F%20then%20errfn%20err%20else%20succfn%20a...%0A%0A%20%20show_user%20%3D%20(id%2C%20cb)%20-%3E%0A%20%20%20%20db.query%20foo%2C%20iferr%20cb%2C%20(user)%20-%3E%0A%20%20%20%20%20%20bar http://coffeescript.org/#try:%20%20iferr%20%3D%20(succfn%2C%...)
- spion 13y agoNo its not. I address this immediately afterwards (below "But spion, why don't you wrap your calbacks?"), but basically, this simple wrapper makes stack traces unusable. Bluebird (and also Q) stitches together stack traces from multiple previous events. This is amazingly awesome.