Skip to content

Commit 7c4f9a3

Browse files
ronagtargos
authored andcommitted
stream: allow calling callback before promise
Refs: #39535 PR-URL: #40772 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2ea08e9 commit 7c4f9a3

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

lib/internal/streams/destroy.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,14 @@ function constructNT(stream) {
292292
then.call(
293293
result,
294294
function() {
295-
process.nextTick(onConstruct, null);
295+
if (!called) {
296+
process.nextTick(onConstruct, null);
297+
}
296298
},
297299
function(err) {
298-
process.nextTick(onConstruct, err);
300+
if (!called) {
301+
process.nextTick(onConstruct, err);
302+
}
299303
});
300304
}
301305
}

lib/internal/streams/writable.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,14 @@ function callFinal(stream, state) {
699699
then.call(
700700
result,
701701
function() {
702-
process.nextTick(onFinish, null);
702+
if (!called) {
703+
process.nextTick(onFinish, null);
704+
}
703705
},
704706
function(err) {
705-
process.nextTick(onFinish, err);
707+
if (!called) {
708+
process.nextTick(onFinish, err);
709+
}
706710
});
707711
}
708712
}

test/parallel/test-stream-construct-async-error.js

+1-23
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,6 @@ const {
99
const { setTimeout } = require('timers/promises');
1010
const assert = require('assert');
1111

12-
{
13-
class Foo extends Duplex {
14-
async _construct(cb) {
15-
// eslint-disable-next-line no-restricted-syntax
16-
await setTimeout(common.platformTimeout(1));
17-
cb();
18-
throw new Error('boom');
19-
}
20-
}
21-
22-
const foo = new Foo();
23-
foo.on('error', common.expectsError({
24-
message: 'boom'
25-
}));
26-
foo.on('close', common.mustCall(() => {
27-
assert(foo._writableState.constructed);
28-
assert(foo._readableState.constructed);
29-
}));
30-
}
31-
3212
{
3313
class Foo extends Duplex {
3414
async _destroy(err, cb) {
@@ -98,9 +78,7 @@ const assert = require('assert');
9878

9979
const foo = new Foo();
10080
foo.write('test', common.mustCall());
101-
foo.on('error', common.expectsError({
102-
code: 'ERR_MULTIPLE_CALLBACK'
103-
}));
81+
foo.on('error', common.mustNotCall());
10482
}
10583

10684
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const {
5+
Duplex,
6+
} = require('stream');
7+
const { setTimeout } = require('timers/promises');
8+
9+
{
10+
class Foo extends Duplex {
11+
async _final(callback) {
12+
// eslint-disable-next-line no-restricted-syntax
13+
await setTimeout(common.platformTimeout(1));
14+
callback();
15+
}
16+
17+
_read() {}
18+
}
19+
20+
const foo = new Foo();
21+
foo._write = common.mustCall((chunk, encoding, cb) => {
22+
cb();
23+
});
24+
foo.end('test', common.mustCall());
25+
foo.on('error', common.mustNotCall());
26+
}

0 commit comments

Comments
 (0)