首页 > 解决方案 > 为什么我的递归函数不会被 promise 调用?

问题描述

无论我做什么,我都无法让我的函数被递归调用。我试过返回并存储到一个变量和许多其他东西中,但它只被调用一次?

 var scrape = () => {
    var waitTime = randomIntFromInterval(1000, 10000);

    nightmare.goto('http://...') 
      .wait(waitTime)
      .evaluate(() => {
        var data = [];
        // ... doing work to get data from browser here
        return data
      })
      .end()
      .then((result) => {
        console.log('scraped successfully!' + new Date());
        scrape();
      }
    )
  }

  scrape();

我以为final.then()就意味着promise已经完成并且已经成功了?

更新:

似乎删除.end()已经解决了我的问题。我不完全理解噩梦文档为https://github.com/segmentio/nightmare,它说它完成了任何队列操作。我从一个例子中提取了一些代码,它就end()在那里。我会因为删除它而受到任何影响吗?

标签: javascriptpromisenightmare

解决方案


你可以试试这个流程,

var scrape = () => {
var waitTime = randomIntFromInterval(1000, 10000);

nightmare.goto('http://...') 

  .then((result) => {
    console.log('scraped successfully!' + new Date());

    ////do the end process here////

    scrape();
  }
  )
 }

 scrape();

推荐阅读