Repository: node
Issue: Keypress events may not be triggered correctly when keypress event handler contains async invocations (only on Windows)
Labels: readline,windows

### Version

20.6.1

### Platform

Microsoft Windows NT 10.0.19045.0 x64 (Windows 10 Pro 22H2)

### Subsystem

_No response_

### What steps will reproduce the bug?

The example code is heavily inspired by [Prompts](https://github.com/terkelg/prompts) library as I've struggled the issue while using this lib. I've also created an [issue](https://github.com/terkelg/prompts/issues/402) there, but it seems that the problem is likely in Node.

The example code is roughly re-written Prompts library code that is only responsible for receiving text input, with all irrelevant details omitted. There, the `input` function waits you to enter some text, records and echoes pressed keys, and when Enter key is pressed, the function finishes its execution and returns what user entered.

Please notice that we pass `asyncAction` to the first `input` invocation and only to it — this is the reason of all troubles (though it is expected to work perfectly).

To reproduce the issue:
* Make sure you're doing this on Windows :)
* Run this code
* Enter some text for the first input, make sure that key presses are echoed back, and press Enter
* Do the same for the second input
* Start entering text for the third input. Here is where problems begin, key presses are not echoed back as `keypress` events are not triggered

```javascript
const { stdin, stdout } = require('process');
const readline = require('readline');

// makes input stream emit 'keypress' events
function createReadline(is, keypress) {
  const rl = readline.createInterface({
    input: is,
    escapeCodeTimeout: 50
  });

  readline.emitKeypressEvents(is, rl);

  if (is.isTTY) {
    is.setRawMode(true);
  }
		
  is.on('keypress', keypress);
	
  // return a callback that stops emitting 'keypress' events and frees resources
  return () => {
    is.removeListener('keypress', keypress);
		
    if (is.isTTY) {
      is.setRawMode(false);
    }
		
    rl.close();
  };
}

// determine what is needed to do for a key object sent by 'keypress' event
function getActionName(key) {
  function needFinish(key) {
    return (key.ctrl && ['c', 'd'].includes(key.name)) ||
      key.name == 'escape' ||
      ['enter', 'return'].includes(key.name);
  }

  function doNothing(key) {
    return ['backspace', 'delete', 'abort', 'escape', 'tab', 'pagedown', 'pageup',
      'home', 'end', 'up', 'down', 'right', 'left'].includes(key.name) || key.ctrl;
  } 
	
  if (doNothing(key)) {
    return 'noop';
  }
  if (needFinish(key)) {
    return 'finish';
  }
  return 'add-letter';
}

// listen to keypresses until user presses Enter, and then return concatenated letters
function input(is, os, onEndInput = () => undefined) {
  return new Promise((resolve) => {
    os.write('Enter string letter by letter, then press Enter\n');
		
    let result = '';
    const closeReadline = createReadline(is, keypress);
		
    function keypress(_, key) {
      os.write(`Encountered ${key.name}\n`)
			
      const actionName = getActionName(key);
			
      if (actionName == 'finish') {
        endInput();
      } else if (actionName == 'add-letter') {
        result += key.name;
      }
    };
		
    async function endInput() {
      await onEndInput(); // <--- If onEndInput is an async function, then it causes the described problem
      closeReadline();
      resolve(result);
    };
  });		
}

async function asyncAction() {
  await new Promise((resolve) => setTimeout(resolve, 100));
  // Another example of async invocation that causes issues (node-fetch@2 is used): await fetch('https://jsonplaceholder.typicode.com/posts');
  // An example of code that does NOT cause issues: await Promise.resolve(0);
}

async function main() {
  await input(stdin, stdout, asyncAction);
  await input(stdin, stdout);
  await input(stdin, stdout);
}

main();
```

### How often does it reproduce? Is there a required condition?

Always reproduced, but only on Windows.
I tried running the example on WSL, no issue there.

### What is the expected behavior? Why is that the expected behavior?

All key presses for all user prompts should be triggered correctly and echoed back by the example code

### What do you see instead?

When the program awaits user to enter text for the third prompt, no keypress events are triggered, until user presses Enter. **And only then**, instead of finishing execution, `input` function starts recording user input.

### Additional information

_No response_