javascript - Promise 和异步函数 "borrowing"来自同时运行的 Promise 的变量

标签 javascript node.js asynchronous sails.js waterline

我在辅助函数answerUserTag 中有这个 Waterline 异步调用:

theQuestion = await UserTag.findOne({id: answerObject.tag});

这就是我调用上述助手的方式:

const promises = userTagAnswers.map(userTagAnswer =>
  sails.helpers.answerUserTag.with(
    {
      userTagAnswer: userTagAnswer,
      answeringUserId: inputs.userId,
      userType: inputs.userType
    }));

await Promise.all(promises);

如果只有一个 userTagAnswers(即一个 promise ),它就可以正常工作。但是,如果有两个 userTagAnswers 并且我在帮助程序中的 theQuestion 查询之后设置了一个断点,我会看到以下不合理的值:

answerObject.tag is equaled to 5c338ae267a1983e84340388

theQuestion.id is equaled to 5c27227ac1e60913703f3002

当调用多个 Promise 时,变量之间似乎存在不合逻辑的重叠。

最佳答案

尝试通过 Promise.each() 调试它?由于迭代是连续的,因此您会知道哪个 Promise 困扰着您。

其次,如果您指定辅助函数,那就太好了。

第三,为什么with?如果您检查MDN - with ,它清楚地说明了并且我引用了:

Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues. It has Ambiguity contra. The with statement makes it hard for a human reader or JavaScript compiler to decide whether an unqualified name will be found along the scope chain, and if so, in which object.

关于javascript - Promise 和异步函数 "borrowing"来自同时运行的 Promise 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54236816/

相关文章:

javascript - 如何在嵌套方法定义中引用对象数据结构?

c# - 使用任务重试异步函数——哪种方法更有效?

ajax - jQuery 同步方法调用问题

javascript - 在 typescript 中创建唯一的关键字

javascript - React hooks - 清除超时和间隔的正确方法

node.js - MacOS/Linux 和 Windows 上的 node_modules 文件夹的访问权限

javascript - Node 将 HTML 表达为 PDF

javascript - 使用 Node.js 和浏览器进行 HTTP POST 原始二进制数据,无需使用表单数据

java - 如何在 Java 8 中超时后终止异步运行的线程以及这样做的含义

javascript - 尝试更新我的对象中的 bool 属性的 Typescript 收到 "TypeError: Cannot assign to read only property"