-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
81 lines (75 loc) · 2.42 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*!
* try-catch-core <https://github.com/hybridables/try-catch-core>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var utils = require('./utils')
/**
* > Executes given `fn` and pass results/errors
* to the `callback` if given, otherwise returns
* a thunk. In below example you will see how passing
* custom arguments can be useful and why such options
* exists.
*
* **Example**
*
* ```js
* var tryCatch = require('try-catch-core')
* var options = {
* context: { num: 123, bool: true }
* args: [require('assert')]
* }
*
* // `next` is always there, until
* // you pass `passCallback: false` to options
* tryCatch(function (assert, next) {
* assert.strictEqual(this.num, 123)
* assert.strictEqual(this.bool, true)
* next()
* }, function (err) {
* console.log('done', err)
* })
* ```
*
* @param {Function} `<fn>` function to be called.
* @param {Object} `[opts]` optional options, such as `context` and `args`, passed to [try-catch-callback][]
* @param {Object} `[opts.context]` context to be passed to `fn`
* @param {Array} `[opts.args]` custom argument(s) to be pass to `fn`, given value is arrayified
* @param {Boolean} `[opts.passCallback]` pass `true` if you want `cb` to be passed to `fn` args.
* @param {Function} `[cb]` callback with `cb(err, res)` signature.
* @return {Function} `thunk` if `cb` not given.
* @throws {TypError} if `fn` not a function.
* @throws {TypError} if no function is passed to `thunk`.
* @api public
*/
module.exports = function tryCatchCore (fn, opts, cb) {
if (typeof fn !== 'function') {
throw new TypeError('try-catch-core: expect `fn` to be a function')
}
if (typeof opts === 'function') {
cb = opts
opts = null
}
if (typeof cb !== 'function') {
return function thunk (done) {
tryCatch.call(this, fn, opts, done)
}
}
tryCatch.call(this, fn, opts, cb)
}
function tryCatch (fn, opts, cb) {
if (typeof cb !== 'function') {
throw new TypeError('try-catch-core: expect `cb` to be a function')
}
var isAsyncFn = utils.isAsync(fn)
cb = isAsyncFn
? utils.once(utils.dezalgo(cb))
: utils.once(cb)
opts = utils.extend({}, opts)
opts.passCallback = typeof opts.passCallback === 'boolean'
? opts.passCallback
: isAsyncFn // if `fn` is async, pass callback automatically
utils.tryCatchCallback.call(this, fn, opts, cb)
}