Skip to content

Instantly share code, notes, and snippets.

View kenOfYugen's full-sized avatar

Kyriakos-Ioannis D. Kyriakou kenOfYugen

View GitHub Profile
@kenOfYugen
kenOfYugen / gist:969cee48e1bf10fac5cf605c21b2fa7d
Created August 13, 2016 13:15 — forked from shesek/gist:3929926
Higher order functions for nodejs-style callbacks error handling

I've used promises for quite some time, but ended up going back to nodejs-style callbacks and creating a bunch of higher-order functions that makes handling that easier and reduce the boilerplate code. The two most useful ones, that really made it much easier, deal with error handling/bubbling: (CoffeeScript)

iferr = (errfn, succfn) -> (e, a...) -> if e? then errfn e else succfn a...
throwerr = iferr.bind null, (e) -> throw e


# To let an error bubble up, `iferr` decorates the success function and the error function, and decide which one should handle the response
# This replaces `if (err) return fn err` that you see all over the place