javascript 未在 Promise 的 .then() 部分中设置变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47112633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:12:22  来源:igfitidea点击:

Variables are not being set inside the .then() part of a Promise

javascriptnode.js

提问by WelpNathan

I'm pretty new to JavaScript and node.js and so being thrown into Promises is pretty daunting when it is required for my bot to function.

我对 JavaScript 和 node.js 还很陌生,所以当我的机器人需要它运行时,被扔进 Promises 是非常令人生畏的。

var storedUserID;
ROBLOX.getIdFromUsername(accountArg).then(function(userID) {
  message.reply("your id is " + userID);
  storedUserID = userID;
});
message.reply(storedUserID);

This is essentially what I have written out, it creates a variable called 'storedUserID' which I would like to update later. I am trying to update it in the Promise but it does not seem to work.

这基本上是我写出来的,它创建了一个名为“ storedUserID”的变量,我想稍后更新它。我正在尝试在 Promise 中更新它,但它似乎不起作用。

In the code, there is message.reply("your id is " + userID);which works as expected. It will print out to a user 'your id is [NUMBER]' so I know userID is not null.

代码中有message.reply("your id is " + userID); 它按预期工作。它会向用户打印“您的 id 是 [NUMBER]”,所以我知道 userID 不为空。

However, when I run message.reply(storedUserID);outside of the Promise, nothing is printed as the variable is not saved. I am not sure why.

但是,当我运行message.reply(storedUserID); 在 Promise 之外,由于未保存变量,因此不会打印任何内容。我不知道为什么。

Any help would be appreciated as this is going towards my work in college! Thank you!

任何帮助将不胜感激,因为这对我在大学的工作有帮助!谢谢!

采纳答案by guest271314

returnthe value at function passed to .then()

return传递给函数的值 .then()

var storedUserID = ROBLOX.getIdFromUsername(accountArg)
                   .then(function(userID) {
                     return userID;
                   });

you can then use or change the Promisevalue when necessary

然后您可以Promise在必要时使用或更改该值

storedUserID = storedUserID.then(id) {
  return /* change value of Promise `storedUserID` here */
});

access and pass the value to message.reply()within .then()

访问并将值传递给message.reply().then()

storedUserID.then(function(id) {
  message.reply(id);
});

or

或者

var storedUserID = ROBLOX.getIdFromUsername(accountArg);
// later in code
storedUserID.then(function(id) {
  message.reply(id);
});

回答by Nathan Heffley

Because the getIdFromUsername is asynchronous, the final line where you message.reply the stored ID is run before the ID is stored.

因为 getIdFromUsername 是异步的,所以在存储 ID 之前运行 message.reply 存储 ID 的最后一行。

Asynchronous functions like getIdFromUsername don't stop the following lines of code from running. If you want it to happen after the ID is returned it needs to go inside the thencallback.

getIdFromUsername 等异步函数不会停止运行以下代码行。如果您希望它在返回 ID 后发生,则需要进入then回调。