Skip to content

Commit efd5e0e

Browse files
tniessendanielleadams
authored andcommitted
test: simplify ReplStream.wait()
PR-URL: #43857 Reviewed-By: Luigi Pinca <[email protected]>
1 parent 0b5dbb2 commit efd5e0e

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

test/parallel/test-repl-preview.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5+
const events = require('events');
56
const { REPLServer } = require('repl');
67
const { Stream } = require('stream');
78
const { inspect } = require('util');
@@ -32,26 +33,16 @@ class REPLStream extends Stream {
3233
if (chunkLines.length > 1) {
3334
this.lines.push(...chunkLines.slice(1));
3435
}
35-
this.emit('line');
36+
this.emit('line', this.lines[this.lines.length - 1]);
3637
return true;
3738
}
38-
wait() {
39+
async wait() {
3940
this.lines = [''];
40-
return new Promise((resolve, reject) => {
41-
const onError = (err) => {
42-
this.removeListener('line', onLine);
43-
reject(err);
44-
};
45-
const onLine = () => {
46-
if (this.lines[this.lines.length - 1].includes(PROMPT)) {
47-
this.removeListener('error', onError);
48-
this.removeListener('line', onLine);
49-
resolve(this.lines);
50-
}
51-
};
52-
this.once('error', onError);
53-
this.on('line', onLine);
54-
});
41+
for await (const [line] of events.on(this, 'line')) {
42+
if (line.includes(PROMPT)) {
43+
return this.lines;
44+
}
45+
}
5546
}
5647
pause() {}
5748
resume() {}

test/parallel/test-repl-top-level-await.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44
const ArrayStream = require('../common/arraystream');
55
const assert = require('assert');
6+
const events = require('events');
67
const { stripVTControlCharacters } = require('internal/util/inspect');
78
const repl = require('repl');
89

@@ -27,31 +28,21 @@ class REPLStream extends ArrayStream {
2728
if (chunkLines.length > 1) {
2829
this.lines.push(...chunkLines.slice(1));
2930
}
30-
this.emit('line');
31+
this.emit('line', this.lines[this.lines.length - 1]);
3132
if (callback) callback();
3233
return true;
3334
}
3435

35-
wait() {
36+
async wait() {
3637
if (this.waitingForResponse) {
3738
throw new Error('Currently waiting for response to another command');
3839
}
3940
this.lines = [''];
40-
return new Promise((resolve, reject) => {
41-
const onError = (err) => {
42-
this.removeListener('line', onLine);
43-
reject(err);
44-
};
45-
const onLine = () => {
46-
if (this.lines[this.lines.length - 1].includes(PROMPT)) {
47-
this.removeListener('error', onError);
48-
this.removeListener('line', onLine);
49-
resolve(this.lines);
50-
}
51-
};
52-
this.once('error', onError);
53-
this.on('line', onLine);
54-
});
41+
for await (const [line] of events.on(this, 'line')) {
42+
if (line.includes(PROMPT)) {
43+
return this.lines;
44+
}
45+
}
5546
}
5647
}
5748

0 commit comments

Comments
 (0)