I have an async.whilst loop, which is for-looping through a string array called hashtagon each cycle.
During the for-loop, searchTwitter() function is fired. I need the for-loop to pause until a callback is returned from the searchTwitter() function, then there's a 2 second pause, then the for-loop continues to the next string in the hashtag array to pass to searchTwitter().
After for loop finishes, there's a timeout in the async.whilst loop, for 10 seconds and then whilst loop reloads the for-loop cycle again.
My code below is firing all the searchTwitter() functions immediately without waiting for callback or setTimeout(callback,2000) :
async.whilst(
function () { return 0 == 0 },
function (callback) {
for (var i = 0; i < hashtag.length; i++) {
searchTwitter(hashtag[i]);
setTimeout(callback, 2000);
}
setTimeout(callback, 10000);
},
function (err) {
}
);
function searchTwitter(tag, callback){
T.get('search/tweets', { q: '#fun', count: 100 }, function(err, data, response) {
console.log(data);
callback();
});
}