[英]make async.waterfall start with argument from another function
I have bumped into a problem that I can't seem to solve. 我遇到了一个似乎无法解决的问题。 This is for a steam trading bot and it works well except for when two people trades with it at the same time because class_id and other_id are global variables and they will change in the middle of a trade if more than one is using it. 这适用于Steam交易机器人,除了两个人同时进行交易之外,它工作得很好,因为class_id和other_id是全局变量,如果使用多个机器人,它们将在交易过程中改变。
I tried defining the variables inside the last if statement but then get_class_id did not find the variables. 我尝试在最后一个if语句中定义变量,但是get_class_id找不到变量。 Is there any way the async function can take item.classid and convert_id_64(offer.accountid_other) directly without defining them as variables? 异步函数可以直接将item.classid和convert_id_64(offer.accountid_other)定义为变量吗? I appreciate any help. 感谢您的帮助。
var class_id
var other_id
function accept_all_trades(offers_recieved) {
offers_recieved.forEach( function(offer) {
if (offer.trade_offer_state == 1) {
if (typeof offer.items_to_give === "accept") {
offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(error, response) {
console.log('accepterat offer');
offer.items_to_receive.forEach(function(item) {
if (item.appid === '420') {
class_id = item.classid;
other_id = convert_id_64(offer.accountid_other);
console.log(class_id);
async.waterfall([get_class_id, get_stack, get_name, get_save], add_names);
}
});
});
}
}
});
}
function get_class_id(callback) {
var test = class_id
callback(null, test)
}
Update 更新资料
I've changed the code to what ben suggested but still when I call get_class_id and try to print the id it is just a blank row in the console, any Ideas? 我已将代码更改为建议的代码,但是当我调用get_class_id并尝试打印id时,它只是控制台中的空白行,有什么想法吗?
function get_class_id(callback) {
console.log(class_id);
var test = class_id;
callback(null, test)
}
The problem here is not aysnc.waterfall(). 这里的问题不是aysnc.waterfall()。 It's async calls (offers.acceptOffer(), get_class_id, get_stack, get_name, get_save, add_names) inside regular javascript forEach(). 它是常规javascript forEach()中的异步调用(offers.acceptOffer(),get_class_id,get_stack,get_name,get_save,add_names)。 You need control-flow loops that can control the flow of those async calls. 您需要可以控制这些异步调用流的控制流循环。 Here is the revised code using async.each(): 这是使用async.each()修改后的代码:
function accept_all_trades(offers_recieved) {
async.each(offers_recieved, function(offer, eachCb) {
if (offer.trade_offer_state !== 1 || typeof offer.items_to_give !== "accept") {
return eachCb(null);
}
offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(error, response) {
console.log('accepterat offer');
async.each(offer.items_to_receive, function(item, eachCb) {
var class_id;
var other_id;
if (item.appid !== '420') {
return eachCb(null);
}
class_id = item.classid;
other_id = convert_id_64(offer.accountid_other);
console.log(class_id);
async.waterfall([
function(waterfallCb) {
var test = class_id;
console.log(class_id);
waterfallCb(null, test);
},
get_stack,
get_name,
get_save,
add_names
], eachCb);
}, function(err) {
console.log(err);
eachCb(null);
});
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.