/
index.js
27 lines (26 loc) 路 969 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const bounce = require('bounce')
/**
* @function
* @public
*
* Handles promises in a save way and returns `[err, result]`.
* In case of success:
* `err = null` and `result = result`.
* In case of an error matching `is`:
* `err = err` and `result = undefined`.
* In case of an error not matching `is`:
* `err = null` and `result = defaults`.
* Error gets logged with `log`.
*
* @param {Promise} promise The promise to be handled
* @param {*} [defaults=undefined] The default result for non-matching errors
* @param {Function} [log=console.error] The log function for non-matching errors
* @param {Function} [is=bounce.isSystem] The error check function
*
* @returns {Array} The result array with `length = 2`
*/
module.exports = (promise, { defaults = undefined, log = console.error, is = bounce.isSystem } = {}) => (
promise
.then(data => [null, data])
.catch(err => is(err) ? [err, undefined] : (log && log(err), [null, defaults]))
)