Javascript 在promise中设置一个变量,并在功能节点外部使用它
我有以下代码。 我试图在.then语句之外使用变量rsname、rslevel和rsexp,但我似乎找不到一种方法来实现这一点。(im使用的模块可在此处找到:)Javascript 在promise中设置一个变量,并在功能节点外部使用它,javascript,node.js,json,Javascript,Node.js,Json,我有以下代码。 我试图在.then语句之外使用变量rsname、rslevel和rsexp,但我似乎找不到一种方法来实现这一点。(im使用的模块可在此处找到:) 您无法使用当前设置。这就是它的本意。不管你对这些变量做什么,都不能离开这个函数 您可以使用如下函数将数据传递到其他地方: const { hiscores } = require('runescape-api'); var rsn = "le me"; function doSomethingWithData(r
您无法使用当前设置。这就是它的本意。不管你对这些变量做什么,都不能离开这个函数 您可以使用如下函数将数据传递到其他地方:
const { hiscores } = require('runescape-api');
var rsn = "le me";
function doSomethingWithData(rsname, rslevel, rsexp) {
console.log(rsname, rslevel, rsexp);
}
var rsnlookup = hiscores.getPlayer(rsn).then(data => {
var rsname = data.name;
var rslevel = data.skills.dungeoneering.level;
var rsexp = data.skills.dungeoneering.experience;
doSomethingWithData(rsname, rslevel, rsexp);
})
rsnlookup.catch((error) => {
console.error("This username doesn\'t exist.");
});
这就是JavaScript的工作原理。无论何时向另一台服务器发出呼叫,JS都不会停止并等待请求返回。它实际上会继续执行,而请求仍然通过internet传递给您。这就是为什么您需要提供一个回调函数,以便它在完成后返回并执行
例如,这里有一个更简单的细分:
// The variables must be declared outside the function for them
// to be accessible from outside the function.
let rsname;
let rslevel;
let resexp;
// This call only BEGINS the request. It will not stay and wait for it.
hiscores.getPlayer(rsn).then(data => {
// This code is only executed after the request comes back with the data,
// so the data can only be used from here.
// These variables do not get declared until this time,
// so they won't exist outside this function until the request is done.
// You need to pass them out to wherever they are needed from here,
// or do all your logic with it right here.
rsname = data.name;
rslevel = data.skills.dungeoneering.level;
rsexp = data.skills.dungeoneering.experience;
});
// This is executed the moment the request begins, it does not wait for it to return.
console.log(rsname); // The variables are still undefined here since the request is not back yet here.
本质上,所有的“异步”意味着代码将开始执行,并在不等待它完成的情况下继续执行。完成后,它会返回执行某种回调函数。根据当前设置,您不能这样做。这就是它的本意。不管你对这些变量做什么,都不能离开这个函数 您可以使用如下函数将数据传递到其他地方:
const { hiscores } = require('runescape-api');
var rsn = "le me";
function doSomethingWithData(rsname, rslevel, rsexp) {
console.log(rsname, rslevel, rsexp);
}
var rsnlookup = hiscores.getPlayer(rsn).then(data => {
var rsname = data.name;
var rslevel = data.skills.dungeoneering.level;
var rsexp = data.skills.dungeoneering.experience;
doSomethingWithData(rsname, rslevel, rsexp);
})
rsnlookup.catch((error) => {
console.error("This username doesn\'t exist.");
});
这就是JavaScript的工作原理。无论何时向另一台服务器发出呼叫,JS都不会停止并等待请求返回。它实际上会继续执行,而请求仍然通过internet传递给您。这就是为什么您需要提供一个回调函数,以便它在完成后返回并执行
例如,这里有一个更简单的细分:
// The variables must be declared outside the function for them
// to be accessible from outside the function.
let rsname;
let rslevel;
let resexp;
// This call only BEGINS the request. It will not stay and wait for it.
hiscores.getPlayer(rsn).then(data => {
// This code is only executed after the request comes back with the data,
// so the data can only be used from here.
// These variables do not get declared until this time,
// so they won't exist outside this function until the request is done.
// You need to pass them out to wherever they are needed from here,
// or do all your logic with it right here.
rsname = data.name;
rslevel = data.skills.dungeoneering.level;
rsexp = data.skills.dungeoneering.experience;
});
// This is executed the moment the request begins, it does not wait for it to return.
console.log(rsname); // The variables are still undefined here since the request is not back yet here.
本质上,所有的“异步”意味着代码将开始执行,并在不等待它完成的情况下继续执行。完成后,它会返回执行某种回调函数。这是否回答了您的问题?同样相关:您可以在外部声明变量并分配它inside@AbhishekRanjan如果代码在不同的时间运行,则不起作用。@VLAZ我查看了您发送给我的链接,但我不确定如何在我的代码中使用此链接。我以前从未真正使用过异步调用。这能回答你的问题吗?同样相关:您可以在外部声明变量并分配它inside@AbhishekRanjan如果代码在不同的时间运行,则不起作用。@VLAZ我查看了您发送给我的链接,但我不确定如何在我的代码中使用此链接。我以前从未真正使用过异步调用。