@@ -112,9 +112,13 @@ function _destroy(self, err, cb) {
112112 }
113113
114114 if ( err ) {
115- process . nextTick ( emitErrorCloseNT , self , err ) ;
115+ queueMicrotask ( ( ) => {
116+ emitErrorCloseNT ( self , err ) ;
117+ } ) ;
116118 } else {
117- process . nextTick ( emitCloseNT , self ) ;
119+ queueMicrotask ( ( ) => {
120+ emitCloseNT ( self ) ;
121+ } ) ;
118122 }
119123 }
120124 try {
@@ -233,7 +237,9 @@ function errorOrDestroy(stream, err, sync) {
233237 r . errored = err ;
234238 }
235239 if ( sync ) {
236- process . nextTick ( emitErrorNT , stream , err ) ;
240+ queueMicrotask ( ( ) => {
241+ emitErrorNT ( stream , err ) ;
242+ } ) ;
237243 } else {
238244 emitErrorNT ( stream , err ) ;
239245 }
@@ -262,7 +268,9 @@ function construct(stream, cb) {
262268 return ;
263269 }
264270
265- process . nextTick ( constructNT , stream ) ;
271+ queueMicrotask ( ( ) => {
272+ constructNT ( stream ) ;
273+ } ) ;
266274}
267275
268276function constructNT ( stream ) {
@@ -291,16 +299,22 @@ function constructNT(stream) {
291299 } else if ( err ) {
292300 errorOrDestroy ( stream , err , true ) ;
293301 } else {
294- process . nextTick ( emitConstructNT , stream ) ;
302+ queueMicrotask ( ( ) => {
303+ emitConstructNT ( stream ) ;
304+ } ) ;
295305 }
296306 }
297307
298308 try {
299309 stream . _construct ( ( err ) => {
300- process . nextTick ( onConstruct , err ) ;
310+ queueMicrotask ( ( ) => {
311+ onConstruct ( err ) ;
312+ } ) ;
301313 } ) ;
302314 } catch ( err ) {
303- process . nextTick ( onConstruct , err ) ;
315+ queueMicrotask ( ( ) => {
316+ onConstruct ( err ) ;
317+ } ) ;
304318 }
305319}
306320
@@ -318,11 +332,14 @@ function emitCloseLegacy(stream) {
318332
319333function emitErrorCloseLegacy ( stream , err ) {
320334 stream . emit ( 'error' , err ) ;
321- process . nextTick ( emitCloseLegacy , stream ) ;
335+ queueMicrotask ( ( ) => {
336+ emitCloseLegacy ( stream ) ;
337+ } ) ;
322338}
323339
324340// Normalize destroy for legacy.
325341function destroyer ( stream , err ) {
342+ process . _rawDebug ( "### 0" )
326343 if ( ! stream || isDestroyed ( stream ) ) {
327344 return ;
328345 }
@@ -345,9 +362,13 @@ function destroyer(stream, err) {
345362 // TODO: Don't lose err?
346363 stream . close ( ) ;
347364 } else if ( err ) {
348- process . nextTick ( emitErrorCloseLegacy , stream , err ) ;
365+ queueMicrotask ( ( ) => {
366+ emitErrorCloseLegacy ( stream , err ) ;
367+ } ) ;
349368 } else {
350- process . nextTick ( emitCloseLegacy , stream ) ;
369+ queueMicrotask ( ( ) => {
370+ emitCloseLegacy ( stream ) ;
371+ } ) ;
351372 }
352373
353374 if ( ! stream . destroyed ) {
0 commit comments