Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么NodeJS*事件循环在我的承诺之后继续?_Javascript_Node.js_Ecmascript 6 - Fatal编程技术网

Javascript 为什么NodeJS*事件循环在我的承诺之后继续?

Javascript 为什么NodeJS*事件循环在我的承诺之后继续?,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,问题 如何让NodeJS在解析所有参数之前只继续事件循环 信息 基于和回答,我得到了工作的承诺,因此当我正在使用的应用程序中使用--密码时。现在,它等待用户输入,不会立即继续NodeJS事件循环 if (argv.password || argv.w) { const p = new Promise((res) => { read({ prompt: 'Password: ', silent: true }, function(er, password) { con

问题

如何让NodeJS在解析所有参数之前只继续事件循环

信息

基于和回答,我得到了工作的
承诺
,因此当我正在使用的应用程序中使用
--密码时。现在,它等待用户输入,不会立即继续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()


您需要处理您的错误:

如果发生错误,则实现拒绝处理程序

if (argv.password || argv.w) {
      const p = new Promise((res, rej) => {
        read({ prompt: 'Password: ', silent: true }, function(er, password) {

          // NEW CODE ADDED HERE
          if (er) rej(er)
          if (argv.update || argv.u) {
            cUpdate(argv.input, config, proj, argv.username, password)
          }

          res(password);
        })
      });
      p.then((pw) => console.log(pw)).catch(err=> console.log(err))
    }

原因是处理程序中的某个地方出现了错误,我甚至怀疑承诺不在代码中,而是在调用的
read
方法中

为了避免这种情况,请尽可能简单地使用承诺处理程序代码,然后尽可能多地使用
链:

if(argv.password | | argv.w){
const p=新承诺((res)=>{
read({prompt:'Password:',silent:true},函数(er,Password){
res(密码);
})
});
p、 然后((密码)=>{
console.log(密码);
如果(argv.update | | argv.u)
cUpdate(argv.input、配置、项目、argv.username、密码);
});
}
现在我假设
cUpdate
方法失败(看起来不是条件),因此我们需要一些错误处理-首先是在实际提示上,也在cUpdate周围

if(argv.password | | argv.w){
const p=新承诺((res,rej)=>{
read({prompt:'Password:',silent:true},函数(er,Password){
if(er)rej(er);
else res(密码);
})
});
p、 然后((密码)=>{
console.log(密码);
如果(argv.update | | argv.u)
cUpdate(argv.input、配置、项目、argv.username、密码);
})
.catch(e=>console.error(e));
}
现在,您可能会在那里看到错误,例如,它来自缺少的
config
proj
条目

我还假设错误可能来自
cUpdate
方法的内部,它也可能是异步的,但是由于您不想在任何地方捕获错误,也不处理承诺,因此它会在那里显示警告。幸运的是,
then
方法允许您返回另一个
Promise
,它也将被处理。我还将使用
promisify
简化读取处理程序,如下所示:

const\u read=require(“util”).promisify(read);
if(argv.password | | argv.w){
_read({prompt:'Password:',silent:true})//这将基本上处理错误并使用密码解决
。然后(密码=>{
console.log(密码);
如果(argv.update | | argv.u)
//如果它是一个承诺,那么在这里返回方法的输出所需要的就是它。
返回cUpdate(argv.input,config,proj,argv.username,password);
})
.catch(e=>{
控制台错误(e);
process.exit(10);//这将允许您在命令行中看到错误
});
}

您认为最后两个代码片段中的哪一个是最好的使用方法?@SandraSchlichting肯定是最后一个带有
promisify
的代码片段。您还可以稍微调整一下,使
中的
if
中,然后
回调在外部(只有在设置了
argv.update
的情况下,才可以使用
cUpdate
进行
回调。
if (argv.password || argv.w) {
      const p = new Promise((res, rej) => {
        read({ prompt: 'Password: ', silent: true }, function(er, password) {

          // NEW CODE ADDED HERE
          if (er) rej(er)
          if (argv.update || argv.u) {
            cUpdate(argv.input, config, proj, argv.username, password)
          }

          res(password);
        })
      });
      p.then((pw) => console.log(pw)).catch(err=> console.log(err))
    }