Odd behavior with asynchronous console I/O in REPL
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 122k
- Forks
- 37.3k
- Avg merge
- 4d 2h
- Merged PRs (30d)
- 283
Description
Version
v16.15.0
Platform
Microsoft Windows NT 10.0.19044.0 x64
Subsystem
No response
What steps will reproduce the bug?
- Open the REPL.
- Run
setTimeout(() => console.log('h'), 1000)and wait for the timer to expire. The letterhwill print where the cursor was, and push the cursor down to the line after the prompt. - Type another letter which results in autofill, such as
g. This already looks somewhat broken, with the letter appearing a few columns to the left of the cursor. - Hit tab to autofill. This is even more broken, as autofill picks up from the cursor rather than from the first letter.
- Hit backspace and watch the word magically fix itself, with a new prompt in the same line as the cursor.
How often does it reproduce? Is there a required condition?
It always occurs when console.log is called asynchronously after the prompt is rendered. process.stdout.write also has somewhat odd behavior, but is noticeably less broken than console.log, possibly because of the lack of a newline character on the end.
What is the expected behavior?
It should be consistent with Promise.reject:
> Promise.reject('h')
Promise {
<rejected> 'h',
[Symbol(async_id_symbol)]: 96,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
> Uncaught 'h'
>
Note that, although this does print Uncaught 'h' after the prompt, it then prints a new prompt immediately. With setTimeout and console.log, the new prompt isn't printed until backspace is pressed.
What do you see instead?
After step 3:

After step 4:

After step 5:

Additional information
Although I discovered this in v16.15.0, it also appears to happen in v14.19.2, v17.9.0, and v18.1.0.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue in the Node.js REPL using setTimeout with console.log, then trace the REPL's asynchronous console I/O and prompt handling. Done means asynchronous output leaves a consistent prompt and preserves typing, autofill, and backspace behavior like the Promise.reject example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100