[英]make async.waterfall start with argument from another function
我遇到了一个似乎无法解决的问题。 这适用于Steam交易机器人,除了两个人同时进行交易之外,它工作得很好,因为class_id和other_id是全局变量,如果使用多个机器人,它们将在交易过程中改变。
我尝试在最后一个if语句中定义变量,但是get_class_id找不到变量。 异步函数可以直接将item.classid和convert_id_64(offer.accountid_other)定义为变量吗? 感谢您的帮助。
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)
}
更新资料
我已将代码更改为建议的代码,但是当我调用get_class_id并尝试打印id时,它只是控制台中的空白行,有什么想法吗?
function get_class_id(callback) {
console.log(class_id);
var test = class_id;
callback(null, test)
}
这里的问题不是aysnc.waterfall()。 它是常规javascript forEach()中的异步调用(offers.acceptOffer(),get_class_id,get_stack,get_name,get_save,add_names)。 您需要可以控制这些异步调用流的控制流循环。 这是使用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.