Saturday, 6 May 2017

Calling a variable amount of asynchronous functions that are dependent on each other

I am simply trying to paginate through a website, where in order to request the next page, the information on the current page is required.

function paginateSite(numPages, cb) {
    var stringToGetToNextPage = ''
    for (var i = 0; i < numPages; i++) {
        (function(i, stringToGetToNextPage) {
            request({
                url: 'http://website.com',
                qs: {
                    nextPage: stringToGetToNextPage
                }
            }, 
            function(err, res, body) {
                body_json = JSON.parse(body)
                console.log(body_json.stringToGetToNextPage)
            })
        })(i, stringToGetToNextPage)
    }
}

As you can see there is no way for me to set stringToGetToNextPage outside of the anonymous function. I have looked through the async library documentation but couldn't find anything that solved my problem.

Has anybody ran into this problem? It seems like this is a case where the async paradigm doesn't have a clean solution.



via Jackson Darrow

No comments:

Post a Comment