@@ -48,7 +48,8 @@ function patchResolveShorthands (prototype) {
4848
4949function wrap ( prefix , fn , expectedArgs , rrtype ) {
5050 const startCh = channel ( prefix + ':start' )
51- const finishCh = channel ( prefix + ':finish' )
51+ const asyncEndCh = channel ( prefix + ':async_end' )
52+ const endCh = channel ( prefix + ':end' )
5253 const errorCh = channel ( prefix + ':error' )
5354
5455 const wrapped = function ( ) {
@@ -67,28 +68,31 @@ function wrap (prefix, fn, expectedArgs, rrtype) {
6768 startArgs . push ( rrtype )
6869 }
6970
70- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
71- return asyncResource . runInAsyncScope ( ( ) => {
72- startCh . publish ( startArgs )
71+ const context = { args : startArgs }
72+ startCh . publish ( context )
7373
74- arguments [ arguments . length - 1 ] = asyncResource . bind ( function ( error , result ) {
75- if ( error ) {
76- errorCh . publish ( error )
77- }
78- finishCh . publish ( result )
79- cb . apply ( this , arguments )
80- } )
74+ arguments [ arguments . length - 1 ] = function ( error , result ) {
75+ if ( error ) {
76+ context . error = error
77+ errorCh . publish ( context )
78+ }
79+ context . result = result
80+ asyncEndCh . publish ( context )
81+ cb . apply ( this , arguments )
82+ }
8183
82- try {
83- return fn . apply ( this , arguments )
84+ try {
85+ return fn . apply ( this , arguments )
8486 // TODO deal with promise versions when we support `dns/promises`
85- } catch ( error ) {
86- error . stack // trigger getting the stack at the original throwing point
87- errorCh . publish ( error )
88-
89- throw error
90- }
91- } )
87+ } catch ( error ) {
88+ error . stack // trigger getting the stack at the original throwing point
89+ context . error = error
90+ errorCh . publish ( context )
91+
92+ throw error
93+ } finally {
94+ endCh . publish ( context )
95+ }
9296 }
9397
9498 return shimmer . wrap ( fn , wrapped )
0 commit comments