递归调用节点js中的api

时间:2018-03-09 18:25:40

标签: javascript node.js api recursion

我正在开发一种爬行应用程序。我有一组帐户已完成一些交易。因此,这些账户既有进出交易,也意味着他们已经获得了报酬,而且他们也为其他人支付了费用。如果没有给出帐号我想要递归找到所有的交易,直到我可以收集他们没有为任何人付款的所有帐户

    checkScam: (accountNo) => {

            return new Promise((resolve, reject) => {
                var separateReqPool = {
                    maxSockets: 10
                };
                var options = {
                    method: 'GET',
                    url: 'http://localhost:3000/account/' + accountNo + '/out',
                    pool: separateReqPool
                };

                request(options, function(error, response, body) {
                    if (!error) {
                        bodyJson = JSON.parse(body)
    //if there is no blocks that mean there are no transactions what so ever. if there are blocks it will check if there are any out transaction
                        if (bodyJson.account.blocks.length > 0) {
                            for (var x = 0; x < bodyJson.account.blocks.length; x++) {
                                for (var y = 0; y < bodyJson.account.blocks[x].length; y++) {

//there is no action.to means they have not paid anyone. that mean one end of the transaction is here.if there is to it will again go to all the address in action.to
                                    if (bodyJson.account.blocks[x][y] && bodyJson.account.blocks[x][y].action && bodyJson.account.blocks[x][y].action.to) {
                                        Scam.checkScam(bodyJson.account.blocks[x][y].action.to).then(
                                            (resolve) => {
                                                console.log(resolve)
                                            }).catch(
                                            (err) => {
                                                console.log(err)
                                            })
                                    } else {
                                        resolve({
                                            "reason": "no to",
                                            "body": JSON.stringify(bodyJson.account.blocks[x][y].action),
                                            "account": accountNo
                                        })
                                    }

                                }

                            }
                        } else {

                            resolve({
                                "reason": "no block",
                                "body": JSON.stringify(bodyJson.account),
                                "account": accountNo
                            })
                        }
                        //resolve(bodyJson.account.blocks)
                    } else {
                        reject(error)
                    }
                });

            })

事情在我的函数中,我调用API来获取事务的所有细节。由于这是一个递归函数,可能有1000个事务,因此我收到错误的原因。

{ Error: connect EMFILE 127.0.0.1:3000 - Local (undefined:undefined)
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at internalConnect (net.js:971:16)
    at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1106:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:10)
  code: 'EMFILE',
  errno: 'EMFILE',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3000 }

这是因为它反复调用API而受到抨击。有没有办法可以避免这种情况。

1 个答案:

答案 0 :(得分:0)

如何在api通话之间等待更多时间:

if (bodyJson.account.blocks[x][y] && bodyJson.account.blocks[x][y].action && bodyJson.account.blocks[x][y].action.to) {
    setTimeout(() => {
        Scam.checkScam(bodyJson.account.blocks[x][y].action.to).then(
            (resolve) => {
                console.log(resolve)
            }).catch(
            (err) => {
                console.log(err)
            })
    },500);

}

虽然我不明白你在用console.log(resolve)

做什么