Skip to content

Instantly share code, notes, and snippets.

@shesek
Last active December 14, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shesek/eff0c0abd31ad8457de8 to your computer and use it in GitHub Desktop.
Save shesek/eff0c0abd31ad8457de8 to your computer and use it in GitHub Desktop.
iferr = (fail, succ) -> (err, a...) -> if err? then fail err else succ a...
# example
foo = (cb) ->
fs.readFile something, iferr cb, (contents) ->
# yey, no need to `return cb err if err?` all over the place
@joeytwiddle
Copy link

Javascript version:

function iferr(fail, succ) {
    return function(err, result) {
        if (err) {
            fail(err);
        } else {
            succ(result); // Multiple arguments would require some succ.apply magic
        }
    };
}

// Example
function foo(cb) {
    fs.readFile(something, iferr(cb, function(contents) {
        // No need to check for err here.
        // If there was one, it will be passed to cb and we won't be called.
    }));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment