Repeatedly prompt user until resolved using nodeJS async-await
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a command-line Node.js program, repeatedly prompting the user is usually a control-flow problem, not an input problem. You ask a question, validate the answer, and loop until the answer is acceptable or the user explicitly cancels.
async and await make this pattern straightforward because each prompt can be written like ordinary sequential code. The cleanest modern approach is to use node:readline/promises and wrap the repetition in a small helper.
Build a Reusable Prompt Function
Node provides a promise-based readline API, which fits naturally with await.
Now you can prompt in a loop without nesting callbacks.
Repeat Until Validation Passes
The simplest pattern is while (true) with a validation check and an early return:
This style is effective because the success condition is close to the prompt and the exit path is explicit.
Support Asynchronous Validation
Sometimes validation itself is asynchronous. For example, you may want to check whether a username already exists in a database or whether a file path exists on disk.
Because await works inside the loop, asynchronous validation reads just as clearly as synchronous validation.
Generalize the Pattern
If you need this behavior in several places, extract a helper that takes a validator:
This keeps your command handlers small and makes validation rules reusable.
Handle Cancellation Cleanly
Real programs should let the user quit instead of trapping them in an endless loop. A simple convention is to accept q or exit:
That makes the CLI much friendlier and prevents dead-end interaction loops.
Common Pitfalls
- Mixing callback-style readline code with
asyncfunctions makes control flow harder than it needs to be. - Forgetting to call
rl.close()leaves the process hanging after the work is done. - Throwing validation errors for ordinary bad input can clutter control flow. Returning structured validation results is often cleaner.
- Using recursion instead of a loop can work for small tools, but an explicit loop is simpler to read and avoids unnecessary stack growth.
- Ignoring cancellation means the user has no clear way to back out of the prompt sequence.
Summary
- Use
node:readline/promisesso each prompt can be awaited directly. - Wrap the question in a helper and loop until validation succeeds.
- Asynchronous checks fit naturally inside the same loop.
- A reusable
promptUntilValidhelper keeps CLI code concise. - Always close the readline interface and provide a way to quit.
Related reading
- replication between SQL Server and MySQL server
- Replication slave locking
- Resources about Asynchronous Programming Design Patterns
- REST-Endpoint Async execution without return value
- Replace an image with Ajax on seaside
- Replace carets with HTML superscript markup using Java
- RestController with StreamingResponseBody async.request-timeout not working
- .Result or await after Task.WhenAll
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.