简体   繁体   中英

async parallel callback is not defined

I am getting the error "callback is not defined" when trying to use async.parallel and but I am out of leads as to why. All the examples of async.parallel have the functions inline (like the async doco https://caolan.github.io/async/docs.html#parallel ) but as the functions I want to run have some logic in them I have tried to split them out so they are not inline. This might be my problem but I think it should be possible, I just cannot figure out from the examples how the call backs should work in this situation.

Then intention here is to have two functions go get lists of IDs from different locations then put them together and do some things on the resulting array. I have trimmed the "go do other things" as it seems irrelevant at this stage.

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 ...
        });
}

It looks like the variable "callback" is not in scope when you invoke it. Try adding it to the argument list of those functions.

Like:

function (error, response, body, callback)
...
function (data, callback)

You need to provide array of functions to execute in parallel to async.parallel so you need to do as follows

    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
    });

Essentially all i am changing is getAssetLocationList(callback) to just getAssetLocationList and getPublicStructures(callback) to getPublicStructures in your main function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM