将一个变量传递给forEach循环中的request-promise函数 [英] Passing a variable to a request-promise function inside a forEach loop
问题描述
我打电话给价格数据正在工作的API。但是,我试图将变量 exchange_pair_id
传递给 then()
函数。 b
在forEach循环中,对于每个资产
,exchange_pair_id都是正确的。然而,在中then()
函数,这个变量总是相同的(forEach循环中的最后一个值)。
我试图将 exchange_pair_id
变量传递给然后(function(response){...}
response.forEach(function(asset){
var assets = asset ['assets'];
exchange_pair_id = asset ['exchange_pair_id'];
options.uri = exchange_objects [exchange [0]]。ticker +'?pair ='+ assets [0] + assets [1]; //覆盖请求Kraken API
console.log(exchange_pair_id)// unique
rp(options).then(function(response){
key = Object。 keys(response ['result']);
price_data = response ['result'] [key];
console.log(exchange_pair_id)// duplicate
});
));
事实,即功能在 .then()
中的离子在请求 - 承诺解析后执行,这需要一些时间。与此同时, forEach
-loop完成并将最后一个资产
分配给 exchange_pair_id
- 在你的代码中是一个全局变量,因为缺少正确的声明(见下文)!之后,第一个请求 - 承诺开始解析并执行它们的回调,但此时已经完成 forEach
-loop,所以 exchange_pair_id
将始终记录相同的值。
为了避免 exchange_pair_id
在全局范围内,应该在 var assets = asset ['assets']之后使用
(第二行)。或简单地在,
而不是;
exchange_pair_id = asset ['exchange_pair_id'];
response.forEach(function(asset){
var assets = asset ['assets'],// <使用','而不是';'
exchange_pair_id = asset ['exchange_pair_id'];
options.uri = exchange_objects [exchange [0]]。ticker +'?pair ='+ assets [0] + assets [1]; //覆盖请求Kraken API
console.log(exchange_pair_id)// unique
rp(options).then(function(response){
key = Object。 keys(response ['result']);
price_data = response ['result'] [key];
console.log(exchange_pair_id)// duplicate
});
});
- 声明的变量在执行中受到约束上下文的声明。
( https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/无论如何,我建议您使用 let
来声明变量,因为它可能有时防止代码的不良行为。查看 let
上的MDN文档来了解它的功能 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
I am calling an API for price data which is working correctly. However, I am trying to pass the variable exchange_pair_id
into the then()
function.
Inside the forEach loop the exchange_pair_id is correct for each asset
. However inside the then()
function this variable is always the same (the last value in the forEach loop).
I am trying to pass the exchange_pair_id
variable into then(function(response) {...}
response.forEach(function(asset) {
var assets = asset['assets'];
exchange_pair_id = asset['exchange_pair_id'];
options.uri = exchange_objects[exchange[0]].ticker + '?pair=' + assets[0] + assets[1]; // overwrite to fit the request to Kraken API
console.log(exchange_pair_id) // unique
rp(options).then(function(response) {
key = Object.keys(response['result']);
price_data = response['result'][key];
console.log(exchange_pair_id) // duplicate
});
});
This happens due to the fact, that the function in the .then()
is executed after the request-promise resolves, which takes some time. In the meanwhile the forEach
-loop finishes and assigns the last asset
to exchange_pair_id
- which in your code is a global variable as a proper declaration is missing (see below)! After that the first request-promises begin to resolve and execute their callback, but at this time the forEach
-loop is already finished so exchange_pair_id
will always log the same value.
To avoid that exchange_pair_id
is in the global scope, you should use a ,
instead of ;
after var assets = asset['assets'];
(second line). Or simply add another var in front of exchange_pair_id = asset['exchange_pair_id'];
response.forEach(function(asset) {
var assets = asset['assets'], // <---- use ',' instead of ';'
exchange_pair_id = asset['exchange_pair_id'];
options.uri = exchange_objects[exchange[0]].ticker + '?pair=' + assets[0] + assets[1]; // overwrite to fit the request to Kraken API
console.log(exchange_pair_id) // unique
rp(options).then(function(response) {
key = Object.keys(response['result']);
price_data = response['result'][key];
console.log(exchange_pair_id) // duplicate
});
});
- Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global.
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
Anyway I recommend you to use let
in order to declare variables as it may sometimes prevent undesirable behaviour of your code. See the MDN docs on let
to get to know what it does - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
这篇关于将一个变量传递给forEach循环中的request-promise函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!