[JavaScript] Why async/await is needed for user input?
Why async/await is needed for user input
In web applications, many interactions are driven by events. A button click, a form submission, or a key press — these are all event-based and handled through listeners like .onclick. But there's a challenge when the app needs to behave step-by-step, especially when waiting for the user's response before continuing.
Events Are Passive by Design
Events don’t “wait.” They simply listen for a condition, then fire and exit.
This is perfectly fine when everything is reactive. The action after the event of clicking is simply to display the message. But what if the flow must pause and wait for input, then proceed?
That’s not something events can do on their own.
The Role of Promises
This wraps the event inside a Promise - now the timing of the flow can be controlled based on when the event occurs.
Why `async/await`?
The reason to use async/await becomes clear: it's about controlling the flow.
In the case of multistep input collection in GTPS blog, each step must:
1. Show a question or interface
2. Wait for the user’s input
3. Only then proceed to the next step
Each step waits for the previous
Comments
Post a Comment