async.mapLimit用于发出多个请求

时间:2015-05-22 15:29:34

标签: javascript node.js asynchronous coffeescript

我有这样一句话:

async.mapLimit(urls, 10, getCertificatesFromPage, callback)

其中url是一个带url的数组,getCertificatesFromPage:

getCertificatesFromPage = (url, callback) ->
  request url, (err, res, html) ->
    $ = cheerio.load(html)
    allRows = $('div.search-result-shop')

    objCollector = []

    allRows.each () ->
      links = $(this).find('a')

      obj =
        companyName: $(this).find('.companyname').text()
        pageDetailsUrl: ''
        link: ''

      for link in links
        extractedLink = link.attribs.href
        if extractedLink.includes('http')
          obj.link = extractedLink
        else
          obj.pageDetailsUrl = nameSpace.pageName + extractedLink
      objCollector.push obj
    return objCollector

这不能很好地工作,因为我需要收集每页10页10页的所有对象,但我只收到50 * 10个链接还有更多

如何使用此代码才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

getCertificatesFromPage = (url, callback) ->

您没有在callback功能中调用getCertificatesFromPage。没有它,框架就无法知道工作何时完成。