Skip to content

Commit 1f722a1

Browse files
ronagdanielleadams
authored andcommitted
stream: allow calling callback before promise
Refs: #39535 PR-URL: #40772 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c10b01e commit 1f722a1

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

lib/internal/streams/destroy.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,14 @@ function constructNT(stream) {
285285
then.call(
286286
result,
287287
function() {
288-
process.nextTick(onConstruct, null);
288+
if (!called) {
289+
process.nextTick(onConstruct, null);
290+
}
289291
},
290292
function(err) {
291-
process.nextTick(onConstruct, err);
293+
if (!called) {
294+
process.nextTick(onConstruct, err);
295+
}
292296
});
293297
}
294298
}

lib/internal/streams/writable.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,14 @@ function callFinal(stream, state) {
698698
then.call(
699699
result,
700700
function() {
701-
process.nextTick(onFinish, null);
701+
if (!called) {
702+
process.nextTick(onFinish, null);
703+
}
702704
},
703705
function(err) {
704-
process.nextTick(onFinish, err);
706+
if (!called) {
707+
process.nextTick(onFinish, err);
708+
}
705709
});
706710
}
707711
}

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

Lines changed: 1 addition & 23 deletions
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
{
Lines changed: 26 additions & 0 deletions
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)