diff options
Diffstat (limited to 'lib/_stream_writable.js')
-rw-r--r-- | lib/_stream_writable.js | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 1232742eb9..22aee3236c 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -647,7 +647,7 @@ function needFinish(state) { function callFinal(stream, state) { state.sync = true; state.pendingcb++; - stream._final((err) => { + const result = stream._final((err) => { state.pendingcb--; if (err) { for (const callback of state[kOnFinished].splice(0)) { @@ -664,6 +664,31 @@ function callFinal(stream, state) { process.nextTick(finish, stream, state); } }); + if (result !== undefined && result !== null) { + try { + const then = result.then; + if (typeof then === 'function') { + then.call( + result, + function() { + if (state.prefinished) + return; + state.prefinish = true; + process.nextTick(() => stream.emit('prefinish')); + state.pendingcb++; + process.nextTick(finish, stream, state); + }, + function(err) { + for (const callback of state[kOnFinished].splice(0)) { + process.nextTick(callback, err); + } + process.nextTick(errorOrDestroy, stream, err, state.sync); + }); + } + } catch (err) { + process.nextTick(errorOrDestroy, stream, err, state.sync); + } + } state.sync = false; } |