UNPKG

837 BJavaScriptView Raw
1// Delegates to `succ` on sucecss or to `fail` on error
2// ex: Thing.load(123, iferr(cb, thing => ...))
3const iferr = (fail, succ) => (err, ...a) => err ? fail(err) : succ(...a)
4
5// Like iferr, but also catches errors thrown from `succ` and passes to `fail`
6const tiferr = (fail, succ) => iferr(fail, (...a) => {
7 try { succ(...a) }
8 catch (err) { fail(err) }
9})
10
11// Delegate to the success function on success, throws the error otherwise
12// ex: Thing.load(123, throwerr(thing => ...))
13const throwerr = iferr.bind(null, err => { throw err })
14
15// Prints errors when one is passed, or does nothing otherwise
16// ex: Thing.load(123, printerr)
17const printerr = iferr(err => console.error(err), () => {})
18
19module.exports = exports = iferr
20exports.iferr = iferr
21exports.tiferr = tiferr
22exports.throwerr = throwerr
23exports.printerr = printerr