【发布时间】:2020-08-12 02:51:08
【问题描述】:
问题
如何让 NodeJS 只继续事件循环,直到解析完所有参数?
信息
根据this 和this 的回答,我得到Promise 的工作,所以当--password 用于我正在开发的应用程序中时。现在它等待用户输入并且不会立即继续 NodeJS 事件循环。
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));
}
上面的效果很好,但是当我添加时
if (argv.update || argv.u) {
console.log('Not suppose to see this yet')
cUpdate(argv.input, config, proj, argv.username, password)
}
然后也执行这段代码,这不是我想要的。
如果我将上述内容内联
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));
}
然后我从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.
【问题讨论】:
标签: javascript node.js ecmascript-6