[英]async parallel callback is not defined
嘗試使用async.parallel時出現錯誤“未定義回調”,但是我為什么無法使用。 async.parallel的所有示例均具有內聯函數(例如async doco https://caolan.github.io/async/docs.html#parallel ),但是由於我要運行的函數中包含一些邏輯,因此我嘗試了拆分它們,使它們不內聯。 這可能是我的問題,但我認為應該可行,我無法從示例中弄清楚在這種情況下回叫應該如何工作。
然后,這里的意圖是讓兩個函數從不同位置獲取ID列表,然后將它們放在一起並在結果數組上做一些事情。 我已經修剪了“執行其他操作”,因為在此階段它似乎無關緊要。
var structuresIDArray = [];
function getPublicStructures(callback){
request({
url: 'https://esi.tech.ccp.is/latest/universe/structures/?datasource=tranquility',
method: 'GET'
}, function (error, response, body) {
if(!error && !body.error && response.statusCode === 200){
console.log('getting public locations');
structuresIDArray.concat(JSON.parse(body));
callback(null, structuresIDArray);
}else{
callback(error);
}
});
}
function getAssetLocationList(callback){
db.any('select distinct "location_id" from "public"."charassets" where "location_id" is not null')
.then(function (data) {
for (var i = 0; i < data.length; i++) {
structuresIDArray.push(data[i].location_id);
if(i+1===data.length){
callback(null, structuresIDArray);
}
}
})
.catch(function (err) {
callback(err);
});
}
function main(){
async.parallel([ getAssetLocationList(callback), getPublicStructures(callback) ],
function (err, results) {
if (err) {
throw err;
}
// Go do some other stuff now ...
});
}
當您調用它時,似乎變量“ callback”不在范圍內。 嘗試將其添加到這些函數的參數列表中。
喜歡:
function (error, response, body, callback)
...
function (data, callback)
您需要提供函數數組以與async.parallel
並行執行,因此您需要執行以下操作
async.parallel([getAssetLocationList,getPublicStructures],function(err, results) {
//This callback executes when both the above functions have called their respective callbacks.
//results[0] will contain data from getAssetLocationList
//results[1] will contain data from getPublicStructures
});
基本上所有我改變是getAssetLocationList(callback)
,只是getAssetLocationList
和getPublicStructures(callback)
,以getPublicStructures
在主函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.