@@ -7,33 +7,34 @@ module.exports = thenify
77/**
88 * Turn async functions into promises
99 *
10- * @param {Function } $$__fn__$$
10+ * @param {Function } fn
1111 * @return {Function }
1212 * @api public
1313 */
1414
15- function thenify ( $$__fn__$$ , options ) {
16- assert ( typeof $$__fn__$$ === 'function' )
17- return eval ( createWrapper ( $$__fn__$$ . name , options ) )
15+ function thenify ( fn , options ) {
16+ assert ( typeof fn === 'function' )
17+ return createWrapper ( fn , options )
1818}
1919
2020/**
2121 * Turn async functions into promises and backward compatible with callback
2222 *
23- * @param {Function } $$__fn__$$
23+ * @param {Function } fn
2424 * @return {Function }
2525 * @api public
2626 */
2727
28- thenify . withCallback = function ( $$__fn__$$ , options ) {
29- assert ( typeof $$__fn__$$ === 'function' )
28+ thenify . withCallback = function ( fn , options ) {
29+ assert ( typeof fn === 'function' )
3030 options = options || { }
3131 options . withCallback = true
32- if ( options . multiArgs === undefined ) options . multiArgs = true
33- return eval ( createWrapper ( $$__fn__$$ . name , options ) )
32+ return createWrapper ( fn , options )
3433}
3534
3635function createCallback ( resolve , reject , multiArgs ) {
36+ // default to true
37+ if ( multiArgs === undefined ) multiArgs = true
3738 return function ( err , value ) {
3839 if ( err ) return reject ( err )
3940 var length = arguments . length
@@ -52,29 +53,25 @@ function createCallback(resolve, reject, multiArgs) {
5253 }
5354}
5455
55- function createWrapper ( name , options ) {
56- name = ( name || '' ) . replace ( / \s | b o u n d (? ! $ ) / g, '' )
56+ function createWrapper ( fn , options ) {
5757 options = options || { }
58- // default to true
59- var multiArgs = options . multiArgs !== undefined ? options . multiArgs : true
60- multiArgs = 'var multiArgs = ' + JSON . stringify ( multiArgs ) + '\n'
61-
62- var withCallback = options . withCallback ?
63- 'var lastType = typeof arguments[len - 1]\n'
64- + 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n'
65- : ''
66-
67- return '(function ' + name + '() {\n'
68- + 'var self = this\n'
69- + 'var len = arguments.length\n'
70- + multiArgs
71- + withCallback
72- + 'var args = new Array(len + 1)\n'
73- + 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n'
74- + 'var lastIndex = i\n'
75- + 'return new Promise(function (resolve, reject) {\n'
76- + 'args[lastIndex] = createCallback(resolve, reject, multiArgs)\n'
77- + '$$__fn__$$.apply(self, args)\n'
78- + '})\n'
79- + '})'
58+ var name = fn . name ;
59+ name = ( name || '' ) . replace ( / \s | b o u n d (? ! $ ) / g, '' )
60+ var newFn = function ( ) {
61+ var self = this
62+ var len = arguments . length
63+ if ( options . withCallback ) {
64+ var lastType = typeof arguments [ len - 1 ]
65+ if ( lastType === 'function' ) return fn . apply ( self , arguments )
66+ }
67+ var args = new Array ( len + 1 )
68+ for ( var i = 0 ; i < len ; ++ i ) args [ i ] = arguments [ i ]
69+ var lastIndex = i
70+ return new Promise ( function ( resolve , reject ) {
71+ args [ lastIndex ] = createCallback ( resolve , reject , options . multiArgs )
72+ fn . apply ( self , args )
73+ } )
74+ }
75+ Object . defineProperty ( newFn , 'name' , { value : name } )
76+ return newFn
8077}
0 commit comments