1 | // Delegates to `succ` on sucecss or to `fail` on error
|
2 | // ex: Thing.load(123, iferr(cb, thing => ...))
|
3 | const 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`
|
6 | const 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 => ...))
|
13 | const 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)
|
17 | const printerr = iferr(err => console.error(err), () => {})
|
18 |
|
19 | module.exports = exports = iferr
|
20 | exports.iferr = iferr
|
21 | exports.tiferr = tiferr
|
22 | exports.throwerr = throwerr
|
23 | exports.printerr = printerr
|