Skip to content

Commit 487833a

Browse files
committed
Refactor wrap function for better readability and efficiency
- Removed unnecessary es initialization outside the condition. - Simplified the flow by checking �syncHooks.AsyncResource and unInAsyncScope in a more direct manner. - Enhanced clarity by removing redundant comments and streamlining the code structure. - Maintained the backporting of the AsyncResource.bind static method for compatibility.
1 parent f8b5f40 commit 487833a

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,18 @@ function tryRequireAsyncHooks () {
213213
/**
214214
* Wrap function with async resource, if possible.
215215
* AsyncResource.bind static method backported.
216+
*
217+
* @param {Function} fn
218+
* @returns {Function}
216219
* @private
217220
*/
218221

219222
function wrap (fn) {
220-
var res
221-
222-
// create anonymous resource
223223
if (asyncHooks.AsyncResource) {
224-
res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn')
225-
}
226-
227-
// incompatible node.js
228-
if (!res || !res.runInAsyncScope) {
229-
return fn
224+
var res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn')
225+
if (res.runInAsyncScope) {
226+
return res.runInAsyncScope.bind(res, fn, null)
227+
}
230228
}
231-
232-
// return bound function
233-
return res.runInAsyncScope.bind(res, fn, null)
229+
return fn
234230
}

0 commit comments

Comments
 (0)