Skip to content

Commit a8b21fd

Browse files
aduh95H4ad
andauthored
process: wait for 'exit' before printing result
Co-authored-by: Vinícius Lourenço <contact@viniciusl.com.br> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #52172 Refs: #52077 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 8bd3cb2 commit a8b21fd

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

lib/internal/process/execution.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
119119
});
120120
if (print) {
121121
const { log } = require('internal/console/global');
122-
log(result);
122+
123+
process.on('exit', () => {
124+
log(result);
125+
});
123126
}
124127

125128
if (origModule !== undefined)

test/parallel/test-cli-print-promise.mjs

+62-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL },
2929
code: 0,
3030
signal: null,
3131
stderr: '',
32-
stdout: 'Promise { <pending> }\n',
32+
stdout: 'Promise { 42 }\n',
3333
});
3434
});
3535

@@ -50,7 +50,7 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL },
5050
it('should output something if process exits before promise settles', async () => {
5151
const result = await spawnPromisified(execPath, [
5252
'--print',
53-
'setTimeout(process.exit,100, 0);timers.promises.setTimeout(200)',
53+
'setTimeout(process.exit, 100, 0);timers.promises.setTimeout(200)',
5454
]);
5555

5656
assert.deepStrictEqual(result, {
@@ -61,6 +61,20 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL },
6161
});
6262
});
6363

64+
it('should respect exit code when process exits before promise settles', async () => {
65+
const result = await spawnPromisified(execPath, [
66+
'--print',
67+
'setTimeout(process.exit, 100, 42);timers.promises.setTimeout(200)',
68+
]);
69+
70+
assert.deepStrictEqual(result, {
71+
code: 42,
72+
signal: null,
73+
stderr: '',
74+
stdout: 'Promise { <pending> }\n',
75+
});
76+
});
77+
6478
it('should handle rejected promises', async () => {
6579
const result = await spawnPromisified(execPath, [
6680
'--unhandled-rejections=none',
@@ -87,7 +101,52 @@ describe('--print with a promise', { concurrency: !process.env.TEST_PARALLEL },
87101
code: 0,
88102
signal: null,
89103
stderr: '',
90-
stdout: 'Promise { <pending> }\n',
104+
stdout: 'Promise { <rejected> 1 }\n',
105+
});
106+
});
107+
108+
it('should handle thenable that resolves', async () => {
109+
const result = await spawnPromisified(execPath, [
110+
'--unhandled-rejections=none',
111+
'--print',
112+
'({ then(r) { r(42) } })',
113+
]);
114+
115+
assert.deepStrictEqual(result, {
116+
code: 0,
117+
signal: null,
118+
stderr: '',
119+
stdout: '{ then: [Function: then] }\n',
120+
});
121+
});
122+
123+
it('should handle thenable that rejects', async () => {
124+
const result = await spawnPromisified(execPath, [
125+
'--unhandled-rejections=none',
126+
'--print',
127+
'({ then(_, r) { r(42) } })',
128+
]);
129+
130+
assert.deepStrictEqual(result, {
131+
code: 0,
132+
signal: null,
133+
stderr: '',
134+
stdout: '{ then: [Function: then] }\n',
135+
});
136+
});
137+
138+
it('should handle Promise.prototype', async () => {
139+
const result = await spawnPromisified(execPath, [
140+
'--unhandled-rejections=none',
141+
'--print',
142+
'Promise.prototype',
143+
]);
144+
145+
assert.deepStrictEqual(result, {
146+
code: 0,
147+
signal: null,
148+
stderr: '',
149+
stdout: 'Object [Promise] {}\n',
91150
});
92151
});
93152
});

0 commit comments

Comments
 (0)