javascript - 为什么 NodeJS* 事件循环在我的 Promise 之后继续?

标签 javascript node.js ecmascript-6

问题

如何让 NodeJS 只继续事件循环,直到解析完所有参数?

信息

基于thisthis回答,我的工作有 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.

最佳答案

原因是处理程序中某处存在错误,我什至怀疑 promise 不在您的代码中,而是在您调用的 read 方法中。

为了避免这种情况,请让您的 Promise 处理程序代码尽可能简单,并尽可能使用 then 链:

if (argv.password || argv.w) {
  const p = new Promise((res) => {
    read({ prompt: 'Password: ', silent: true }, function(er, password) {
      res(password);
    })
  });
  p.then((password) => {
    console.log(password); 
    if (argv.update || argv.u)
      cUpdate(argv.input, config, proj, argv.username, password);
  });
}

现在我假设 cUpdate 方法失败(看起来不像是条件),因此我们需要一些错误处理 - 首先在实际提示符上以及 cUpdate 周围。

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

  p.then((password) => {
    console.log(password); 
    if (argv.update || argv.u)
      cUpdate(argv.input, config, proj, argv.username, password);
  })
  .catch(e => console.error(e));
}

现在,如果错误来自于缺少 configproj 条目,您可能会在那里看到错误。

我还假设错误可能来自 cUpdate 方法内部,并且它也可能是异步的,但因为您没有尝试在任何地方捕获错误,并且您不这样做处理它在那里显示警告的 promise 。幸运的是,then 方法允许您返回另一个 Promise 并且它也将被处理。我还使用 promisify 简化读取处理程序,如下所示:

const _read = require("util").promisify(read);

if (argv.password || argv.w) {
  _read({ prompt: 'Password: ', silent: true }) // this will essentially handle the error and resolve with the password
    .then(password => {
      console.log(password); 
      if (argv.update || argv.u)
        // all you need it to return the output of the method here if it's a promise.
        return cUpdate(argv.input, config, proj, argv.username, password);
    })
    .catch(e => {
      console.error(e);
      process.exit(10); // this will allow you to see an error in the command line
    });
}

关于javascript - 为什么 NodeJS* 事件循环在我的 Promise 之后继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61477698/

相关文章:

javascript - 将 Summernote 与 Meteor 结合使用(发现 Meteor)

node.js - 如何将上下文传递到自定义对话框?

javascript - ES6 用于解构和分配对象属性的好东西

javascript - GET 请求适用于浏览器的 REST 客户端,但不适用于 JS

node.js - [NODEJS][AZURE] 如何获取文件的 sastoken url 并能够访问它。 [签名不匹配。]

javascript - EcmaScript 6 中的 Yield 优先级

javascript - 函数参数中变量周围的花括号是什么意思

javascript - 以编程方式禁用 CSS 文本选择

Javascript Math.floor 生成的数字低于预期

javascript - 使用 Lo-Dash 继承