0

I have a array which I have to loop through. I can't use for loop because it's asynchronous and it has callback in. I would have to use a loop which waits for callback. Is that possible?

Code:

        if ( bots[1].getInventory().getItems().length < capacity ){  
            var executed = false;
            bots[1].createDeposit({
                steamid: req.query.steamid,
                token: req.query.token,
                itemsFromThem: uniqueItems,
                message: req.query.message,
                callback: function( err, dbTradeId ){
                    if ( !executed ){
                        executed = true;
                        if ( !err && dbTradeId ){
                            res.json({ result: 1, dbTradeId: dbTradeId });
                        } else {
                            console.log('» Tried to create deposit, but',err);
                            myMessages.push("Problem");
                            res.json({ error: err });                   
                        }
                    }
                }
            });
        } else {
            console.log('» Tried to create deposit, but no bot found(2)');
            myMessages.push("Available bot not found(2)");
        }

My question is not a duplicate because, I don't want it to go through every item in the array. Only until the successful callback has been executed.

Regards

2 Answers2

0

You must take a look at async#each .It allows you to run async calls against a list of array or in a loop and gives you a place to run a method when all of the async calls are done.

// Should give you an idea how to use it
async.each(bots, function (item, callback) {
  if ( item.getInventory().getItems().length < capacity ){  
        var executed = false;
        item.createDeposit({
            steamid: req.query.steamid,
            token: req.query.token,
            itemsFromThem: uniqueItems,
            message: req.query.message,
            callback: function( err, dbTradeId ){
                if ( !executed ){
                    executed = true;
                    if ( !err && dbTradeId ){
                         callback(null, { result: 1, dbTradeId: dbTradeId });
                       // res.json();
                    } else {
                        console.log('» Tried to create deposit, but',err);
                        myMessages.push("Problem");
                        callback(err);                  
                    }
                }
            }
        });
    } else {
        console.log('» Tried to create deposit, but no bot found(2)');
        myMessages.push("Available bot not found(2)");
    }
}, function (err) {
  console.log('All done');
});
digit
  • 4,479
  • 3
  • 24
  • 43
0

You can create another array of promises and then use Promise.all to wait them for completion. I don't known the source of what you have to iterate, but let's assume that you want to make an http connection:

const yourThingies = [
    "http://a-server.com/",
    "http://www.another.com/"
    //And so on...
];
let promises = [];

yourThingies.forEach( (url) => {
    const p = new Promise((resolve, reject) => {
        //Call this on resolve, or use something that returns a promise.
        resolve("your data");
    });
    promises.push(p);
});

Promise.all(promises).then((solvedPromises) => {
   //Do things once all is done
       solvedPromises.forEach((solv) => {
            //So on...
       });
});

Further information about Promise Promise MDN Docs

EDIT: Note: When I've answered there was no code, I'll try to edit.

Sigma Octantis
  • 903
  • 1
  • 8
  • 25