简体   繁体   English

Node.js承诺递归

[英]Node.js Promises recursion

Im having trouble with iterating through my Promises and finishing my parser code: 我在遍历Promises和完成解析器代码时遇到麻烦:

let startFrom = 0,
    totalRecords = 10000,
    doneRecords = 0
const rows = 10

const parserRequests = function () {
if (startFrom <= totalRecords) {
    setTimeout(function () {
        getParserOffers(startFrom)
        startFrom += rows
        parserRequests()
    }, 2000)
}}

const getParserOffers = function (start) {
request({
    uri: domain + '/lucene/search/filter?minPrice=0.00&maxPrice=639.63&stock=all&cat=0&sortOrder=added+desc&start=' + start + '&rows=' + rows + '&steam_app_id=&steam_category=&steam_prod_type=&includeOutOfStock=&includeFreeGames=false',
    json: true
}).then(response => {
    const promises = response.docs.map(doc => getOffersData(doc))
    return Promise.all(promises)
}).then(gamesList => {
    doneRecords += rows
    console.log('Done records: ', doneRecords)
    if (doneRecords >= totalRecords) {
        process.exit()
    }
}).catch(error => {
    console.error('ERROR: Server did not respond')
})}

In recursive parserRequests function I iterate till totalRecords and in getParserOffers after getting gamesList I also iterate every time, when it's done. 在递归的parserRequests函数中,我迭代直到totalRecords,并在获取gamesList之后在getParserOffers中进行迭代,每次完成时我也会进行迭代。 The problem is that doneRecords should also reach 10000 number to execute process.exit() and stop the program, but it never does, it stops for example on 9750. What can be the problem? 问题在于,doneRecords也应该达到10000号才能执行process.exit()并停止程序,但是它从不停止,例如在9750上停止。这可能是什么问题? Thank you for help 谢谢你的帮助

First of all, it doesn't look like getParserOffers() method executes sequentially. 首先,看起来getParserOffers()方法不是顺序执行的。 It is called every 2 seconds, and if the host that you're querying can't handle the eleven HTTP requests per given time, then your requests are queued on the host and your promises stacked waiting. 它每2秒调用一次,并且如果您查询的主机在给定的时间内无法处理十一个HTTP请求,则您的请求将在主机上排队,并且Promise会等待。

I think you meant for it to execute sequentially. 我认为您的意思是按顺序执行。 For that you need to return your promise: 为此,您需要兑现您的承诺:

const getParserOffers = function (start) {
  return request({

and attach to it the next iteration via then() method: 并通过then()方法将其附加到下一次迭代:

if (startFrom <= totalRecords) {
    setTimeout(function () {
        getParserOffers(startFrom).then(_ => {
            startFrom += rows
            parserRequests()
        });
    }, 2000);
}

you might also want to decrease the delay since it would now execute sequentially one after another. 您可能还想减少延迟,因为它现在可以一个接一个地顺序执行。

I would also recommend changing this part: 我还建议更改此部分:

doneRecords += rows
console.log('Done records: ', doneRecords)
if (doneRecords >= totalRecords) {
    process.exit()
}

move it to the place where you set the timeout, you don't have to process.exit(), you could simply stop setting the next timeout and your program would finish executing. 将其移至您设置超时的位置,而不必进行process.exit(),只需停止设置下一个超时即可,程序将完成执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM