@@ -55,22 +55,23 @@ class HookCodeFactory {
5555 } ) ;
5656 let code = "" ;
5757 code += '"use strict";\n' ;
58- code += "return new Promise((_resolve, _reject) => {\n" ;
58+ code += this . header ( ) ;
59+ code += "return new Promise((function(_resolve, _reject) {\n" ;
5960 if ( errorHelperUsed ) {
6061 code += "var _sync = true;\n" ;
6162 code += "function _error(_err) {\n" ;
6263 code += "if(_sync)\n" ;
63- code += "_resolve(Promise.resolve().then(() => { throw _err; }));\n" ;
64+ code +=
65+ "_resolve(Promise.resolve().then((function() { throw _err; })));\n" ;
6466 code += "else\n" ;
6567 code += "_reject(_err);\n" ;
6668 code += "};\n" ;
6769 }
68- code += this . header ( ) ;
6970 code += content ;
7071 if ( errorHelperUsed ) {
7172 code += "_sync = false;\n" ;
7273 }
73- code += "});\n" ;
74+ code += "})) ;\n" ;
7475 fn = new Function ( this . args ( ) , code ) ;
7576 break ;
7677 }
@@ -100,7 +101,16 @@ class HookCodeFactory {
100101 const onError = options . onError ;
101102 const onResult = options . onResult ;
102103 const onDone = options . onDone ;
103- return this . content (
104+ let code = "" ;
105+ for ( let i = 0 ; i < this . options . interceptors . length ; i ++ ) {
106+ const interceptor = this . options . interceptors [ i ] ;
107+ if ( interceptor . call ) {
108+ code += `${ this . getInterceptor ( i ) } .call(${ this . args ( {
109+ before : interceptor . context ? "_context" : undefined
110+ } ) } );\n`;
111+ }
112+ }
113+ code += this . content (
104114 Object . assign ( options , {
105115 onError :
106116 onError &&
@@ -143,6 +153,7 @@ class HookCodeFactory {
143153 } )
144154 } )
145155 ) ;
156+ return code ;
146157 } else {
147158 return this . content ( options ) ;
148159 }
@@ -160,14 +171,6 @@ class HookCodeFactory {
160171 code += "var _taps = this.taps;\n" ;
161172 code += "var _interceptors = this.interceptors;\n" ;
162173 }
163- for ( let i = 0 ; i < this . options . interceptors . length ; i ++ ) {
164- const interceptor = this . options . interceptors [ i ] ;
165- if ( interceptor . call ) {
166- code += `${ this . getInterceptor ( i ) } .call(${ this . args ( {
167- before : interceptor . context ? "_context" : undefined
168- } ) } );\n`;
169- }
170- }
171174 return code ;
172175 }
173176
@@ -227,8 +230,9 @@ class HookCodeFactory {
227230 break ;
228231 case "async" :
229232 let cbCode = "" ;
230- if ( onResult ) cbCode += `(_err${ tapIndex } , _result${ tapIndex } ) => {\n` ;
231- else cbCode += `_err${ tapIndex } => {\n` ;
233+ if ( onResult )
234+ cbCode += `(function(_err${ tapIndex } , _result${ tapIndex } ) {\n` ;
235+ else cbCode += `(function(_err${ tapIndex } ) {\n` ;
232236 cbCode += `if(_err${ tapIndex } ) {\n` ;
233237 cbCode += onError ( `_err${ tapIndex } ` ) ;
234238 cbCode += "} else {\n" ;
@@ -239,7 +243,7 @@ class HookCodeFactory {
239243 cbCode += onDone ( ) ;
240244 }
241245 cbCode += "}\n" ;
242- cbCode += "}" ;
246+ cbCode += "}) " ;
243247 code += `_fn${ tapIndex } (${ this . args ( {
244248 before : tap . context ? "_context" : undefined ,
245249 after : cbCode
@@ -252,15 +256,15 @@ class HookCodeFactory {
252256 } ) } );\n`;
253257 code += `if (!_promise${ tapIndex } || !_promise${ tapIndex } .then)\n` ;
254258 code += ` throw new Error('Tap function (tapPromise) did not return promise (returned ' + _promise${ tapIndex } + ')');\n` ;
255- code += `_promise${ tapIndex } .then(_result${ tapIndex } => {\n` ;
259+ code += `_promise${ tapIndex } .then((function( _result${ tapIndex } ) {\n` ;
256260 code += `_hasResult${ tapIndex } = true;\n` ;
257261 if ( onResult ) {
258262 code += onResult ( `_result${ tapIndex } ` ) ;
259263 }
260264 if ( onDone ) {
261265 code += onDone ( ) ;
262266 }
263- code += `}, _err${ tapIndex } => {\n` ;
267+ code += `}), function( _err${ tapIndex } ) {\n` ;
264268 code += `if(_hasResult${ tapIndex } ) throw _err${ tapIndex } ;\n` ;
265269 code += onError ( `_err${ tapIndex } ` ) ;
266270 code += "});\n" ;
@@ -322,7 +326,7 @@ class HookCodeFactory {
322326 const syncOnly = this . options . taps . every ( t => t . type === "sync" ) ;
323327 let code = "" ;
324328 if ( ! syncOnly ) {
325- code += "var _looper = () => {\n" ;
329+ code += "var _looper = (function() {\n" ;
326330 code += "var _loopAsync = false;\n" ;
327331 }
328332 code += "var _loop;\n" ;
@@ -363,7 +367,7 @@ class HookCodeFactory {
363367 code += "} while(_loop);\n" ;
364368 if ( ! syncOnly ) {
365369 code += "_loopAsync = true;\n" ;
366- code += "};\n" ;
370+ code += "}) ;\n" ;
367371 code += "_looper();\n" ;
368372 }
369373 return code ;
@@ -388,9 +392,9 @@ class HookCodeFactory {
388392 code += "do {\n" ;
389393 code += `var _counter = ${ this . options . taps . length } ;\n` ;
390394 if ( onDone ) {
391- code += "var _done = () => {\n" ;
395+ code += "var _done = (function() {\n" ;
392396 code += onDone ( ) ;
393- code += "};\n" ;
397+ code += "}) ;\n" ;
394398 }
395399 for ( let i = 0 ; i < this . options . taps . length ; i ++ ) {
396400 const done = ( ) => {
0 commit comments