diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 6f319fa564983e..160dbce8d8ee47 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -734,17 +734,10 @@ function finishMaybe(stream, state, sync) { if (needFinish(state)) { prefinish(stream, state); if (state.pendingcb === 0) { + state.pendingcb++; if (sync) { - state.pendingcb++; - process.nextTick((stream, state) => { - if (needFinish(state)) { - finish(stream, state); - } else { - state.pendingcb--; - } - }, stream, state); - } else if (needFinish(state)) { - state.pendingcb++; + process.nextTick(finish); + } else { finish(stream, state); } } @@ -753,6 +746,11 @@ function finishMaybe(stream, state, sync) { function finish(stream, state) { state.pendingcb--; + + if (!needFinish(state)) { + return; + } + state.finished = true; const onfinishCallbacks = state[kOnFinished].splice(0);