-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (68 loc) · 2.17 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
/*!
* to-callback <https://github.com/tunnckoCore/to-callback>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var tryCatch = require('try-catch-callback')
/**
* > Gets a `fn` function, that has _"result-first"_
* callback and return same function, but with _"error-first"_
* callback, like is the convention at node.js land.
* Such APIs that has _"result-first"_ callbacks are for example
* the eventemitter's (streams too) `.on` method, which should
* have two arguments `eventName` and _"result-first"_ callback,
* that gets as many arguments as `.emit` sends.
*
* **Example**
*
* ```js
* var EventEmitter3 = require('eventemitter3')
* var toCallback = require('to-callback')
* var emitter = new EventEmitter3()
*
* // classic "result-first" case
* emitter.on('foo', function (a, b) {
* console.log(a, b) // => 1, 2
* })
*
* var onWithCallback = toCallback(emitter.on.bind(emitter))
*
* // node.js's "error-first" style
* onWithCallback('foo', function (err, a, b) {
* console.log(err, a, b) // => null, 1, 2
* })
*
* emitter.emit('foo', 1, 2)
* ```
*
* @name toCallback
* @param {Function} `<fn>` function that has _"result-first"_ callback
* @return {Function} function with same arguments, but instead of _"result-first"_
* callback it has _"error-first"_ callback
* @api public
*/
module.exports = function resultFirst2callback (fn) {
return function errorFirstFn () {
var args = [].slice.call(arguments)
var errFirstCb = args[args.length - 1]
if (typeof errFirstCb !== 'function') {
throw new TypeError('to-callback: expect last argument to be function')
}
if (errFirstCb.length < 1) {
throw new Error('to-callback: expect error-first callback')
}
// called when no errors
function resFirstCb (/* arg1, arg2, arg3 */) {
var args = [].slice.call(arguments)
errFirstCb.apply(null, [null].concat(args))
}
tryCatch(fn, {
args: args.slice(0, -1).concat(resFirstCb)
}, function (err) {
// called when execution of `fn` fails
if (err) errFirstCb(err)
})
}
}