1

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

}

2 Answers 2

2
async.whilst(
    function () { return 0 == 0 },
    function (callback) {
        async.eachSeries(hashtag, searchTwitter, callback);
    },
    function (err) {
    }
);

function searchTwitter(tag, done) {
    // search for `tag`
    // call `done` when done
}

Read docs: async#eachSeries

Sign up to request clarification or add additional context in comments.

Comments

1

Like @Anatoliy mentioned in his answer, async.eachSeries is going to help you out with that for loop. It will loop over your hashtag array, calling the next item only after you've called the callback function.

The trick to getting those delays in place is to call your setTimeout functions inside the callbacks from searchTwitter and the completion callback from eachSeries. Like so:

async.whilst(
    function () { return true }, // true makes more sense than 0 == 0, especially from a readability standpoint
    function (callback) {
        async.eachSeries(hashtag, function(tag, callback) {
            searchTwitter(tag, function() {
                setTimeout(callback, 2000); // Wait 2 seconds before going on to the next tag
            })
        }, function(err) {
            setTimeout(callback, 10000); // Wait 10 seconds before looping over the hashtags again
        });
    },
    function (err) {
    }
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.