如何使用axios.all兑现所有承诺?

时间:2020-04-02 01:19:20

标签: node.js promise axios

我正在使用axioscheerio抓取网页:
该网页具有许多链接,而向下滚动时则具有更多的负载(例如facebook)。
我想在 的同时向下滚动,直到到达末尾。
这是我的代码示例:

cheerio = require('cheerio')
axios = require('axios')

function getLink(id) {
    return axios(options).then(function(response) {
        // Do stuff...
    })
}

function scrollDown() {
    axios(scrollOptions).then(function(response) {
        $ = cheerio.load(response['data'])
        isScrollFinished = ($('.page_more').length == 0)
        promises = []
        newLinks = $('.link') // Get the new links that were loaded while scrolling
        newLinks.each(function() {
            promises.push(getLink($(this).attr('id')))
        })
        axios.all(promises).then(responseArr => {
            if(isScrollFinished) {
                // Exit script
            }
        })
        if(!isScrollFinished) {
            scrollDown()
        }
    })
}

scrollDown()

此代码的问题是有时它在我退出之前不会抓取所有链接。
这是因为最后一个axios.all只等到刮擦最后一个滚动页面的所有链接之后。
我该如何解决?

1 个答案:

答案 0 :(得分:0)

我将promises数组创建为静态变量,并且在滚动结束时仅在其上调用axios.all:

cheerio = require('cheerio')
axios = require('axios')

function getLink(id) {
    return axios(options).then(function(response) {
        // Do stuff...
    })
}

function scrollDown() {
    if (typeof scrollDown.promises === 'undefined') { 
        scrollDown.promises = [] // Define static variable if undefined
    }
    axios(scrollOptions).then(function(response) {
        $ = cheerio.load(response['data'])
        isScrollFinished = ($('.page_more').length == 0)
        newLinks = $('.link') // Get the new links that were loaded while scrolling
        newLinks.each(function() {
            scrollDown.promises.push(getLink($(this).attr('id')))
        })
        if(isScrollFinished) {
            axios.all(scrollDown.promises).then(responseArr => {
                // Exit script
            })
        }
        else {
            scrollDown()
        }
    })
}

scrollDown()

更好的解决方案将很乐意被接受。