Question
How can I get NodeJS to only continue the event loop until all arguments have been parsed?
Info
Based on this and this answer, I got Promise
for work, so when --password
is used in the app I am working on. Now it waits for the users input and doesn't continue the NodeJS event loop right away.
if (argv.password || argv.w) {
const p = new Promise((res) => {
read({ prompt: 'Password: ', silent: true }, function(er, password) {
console.log('Your password is: %s', password);
res(password);
})
});
p.then((pw) => console.log(pw));
}
The above works perfectly, but when I add
if (argv.update || argv.u) {
console.log('Not suppose to see this yet')
cUpdate(argv.input, config, proj, argv.username, password)
}
then also executes this code, which is not what I want.
If I inline the above
if (argv.password || argv.w) {
const p = new Promise((res) => {
read({ prompt: 'Password: ', silent: true }, function(er, password) {
// NEW CODE ADDED HERE
if (argv.update || argv.u) {
cUpdate(argv.input, config, proj, argv.username, password)
}
res(password);
})
});
p.then((pw) => console.log(pw));
}
then I get this error from cUpdate()
(node:9005) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:9005) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.