Skip to content

Commit dbcf465

Browse files
committed
process: wait promise resolve before print result
1 parent f9755f6 commit dbcf465

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

lib/internal/process/execution.js

Lines changed: 4 additions & 1 deletion
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', async () => {
124+
log(await result);
125+
});
123126
}
124127

125128
if (origModule !== undefined)

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('--print with a promise', { concurrency: true }, () => {
1515
code: 0,
1616
signal: null,
1717
stderr: '',
18-
stdout: 'Promise { 42 }\n',
18+
stdout: '42\n',
1919
});
2020
});
2121

@@ -29,7 +29,7 @@ describe('--print with a promise', { concurrency: true }, () => {
2929
code: 0,
3030
signal: null,
3131
stderr: '',
32-
stdout: 'Promise { <pending> }\n',
32+
stdout: '42\n',
3333
});
3434
});
3535

@@ -43,7 +43,7 @@ describe('--print with a promise', { concurrency: true }, () => {
4343
code: 0,
4444
signal: null,
4545
stderr: '',
46-
stdout: 'Promise { <pending> }\n',
46+
stdout: '',
4747
});
4848
});
4949

@@ -57,11 +57,11 @@ describe('--print with a promise', { concurrency: true }, () => {
5757
code: 0,
5858
signal: null,
5959
stderr: '',
60-
stdout: 'Promise { <pending> }\n',
60+
stdout: '',
6161
});
6262
});
6363

64-
it('should handle rejected promises', async () => {
64+
it('should handle rejected promises with unhandled-rejections=none', async () => {
6565
const result = await spawnPromisified(execPath, [
6666
'--unhandled-rejections=none',
6767
'--print',
@@ -72,11 +72,11 @@ describe('--print with a promise', { concurrency: true }, () => {
7272
code: 0,
7373
signal: null,
7474
stderr: '',
75-
stdout: 'Promise { <rejected> 1 }\n',
75+
stdout: '',
7676
});
7777
});
7878

79-
it('should handle promises that reject after one tick', async () => {
79+
it('should handle promises that reject after one tick with unhandled-rejections=none', async () => {
8080
const result = await spawnPromisified(execPath, [
8181
'--unhandled-rejections=none',
8282
'--print',
@@ -87,7 +87,32 @@ describe('--print with a promise', { concurrency: true }, () => {
8787
code: 0,
8888
signal: null,
8989
stderr: '',
90-
stdout: 'Promise { <pending> }\n',
90+
stdout: '',
9191
});
9292
});
93+
94+
it('should error with unhandled rejected promises', async () => {
95+
const result = await spawnPromisified(execPath, [
96+
'--print',
97+
'Promise.reject(1)',
98+
]);
99+
100+
assert.strictEqual(result.code, 1);
101+
assert.strictEqual(result.signal, null);
102+
assert.strictEqual(result.stdout, '');
103+
assert.ok(result.stderr.includes('ERR_UNHANDLED_REJECTION'), 'Not found ERR_UNHANDLED_REJECTION');
104+
});
105+
106+
it('should error when throw inside fn', async () => {
107+
const result = await spawnPromisified(execPath, [
108+
'--print',
109+
'Promise.resolve().then(()=>{throw new Error(10)})',
110+
]);
111+
112+
assert.strictEqual(result.code, 1);
113+
assert.strictEqual(result.signal, null);
114+
assert.strictEqual(result.stdout, '');
115+
assert.ok(result.stderr.includes('throw new Error(10)'), `Found: ${result.stderr}`);
116+
assert.ok(result.stderr.includes('Error: 10'), `Found: ${result.stderr}`);
117+
});
93118
});

0 commit comments

Comments
 (0)