stream: allow calling callback before promise

Refs: https://github.com/nodejs/node/issues/39535

PR-URL: https://github.com/nodejs/node/pull/40772
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Robert Nagy
2021-11-10 10:14:48 +02:00
parent cf56abe6bb
commit afe460ec9e
4 changed files with 39 additions and 27 deletions

View File

@@ -292,10 +292,14 @@ function constructNT(stream) {
then.call(
result,
function() {
process.nextTick(onConstruct, null);
if (!called) {
process.nextTick(onConstruct, null);
}
},
function(err) {
process.nextTick(onConstruct, err);
if (!called) {
process.nextTick(onConstruct, err);
}
});
}
}

View File

@@ -699,10 +699,14 @@ function callFinal(stream, state) {
then.call(
result,
function() {
process.nextTick(onFinish, null);
if (!called) {
process.nextTick(onFinish, null);
}
},
function(err) {
process.nextTick(onFinish, err);
if (!called) {
process.nextTick(onFinish, err);
}
});
}
}

View File

@@ -9,26 +9,6 @@ const {
const { setTimeout } = require('timers/promises');
const assert = require('assert');
{
class Foo extends Duplex {
async _construct(cb) {
// eslint-disable-next-line no-restricted-syntax
await setTimeout(common.platformTimeout(1));
cb();
throw new Error('boom');
}
}
const foo = new Foo();
foo.on('error', common.expectsError({
message: 'boom'
}));
foo.on('close', common.mustCall(() => {
assert(foo._writableState.constructed);
assert(foo._readableState.constructed);
}));
}
{
class Foo extends Duplex {
async _destroy(err, cb) {
@@ -98,9 +78,7 @@ const assert = require('assert');
const foo = new Foo();
foo.write('test', common.mustCall());
foo.on('error', common.expectsError({
code: 'ERR_MULTIPLE_CALLBACK'
}));
foo.on('error', common.mustNotCall());
}
{

View File

@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const {
Duplex,
} = require('stream');
const { setTimeout } = require('timers/promises');
{
class Foo extends Duplex {
async _final(callback) {
// eslint-disable-next-line no-restricted-syntax
await setTimeout(common.platformTimeout(1));
callback();
}
_read() {}
}
const foo = new Foo();
foo._write = common.mustCall((chunk, encoding, cb) => {
cb();
});
foo.end('test', common.mustCall());
foo.on('error', common.mustNotCall());
}