A
A
a2016-10-22 18:19:02
JavaScript
a, 2016-10-22 18:19:02

How to correctly set recursion for map in nodejs?

There is this code:

longPoll(callback) {
    let longPollMap = new Map();
  this.arg = 0;
    longPollMap.set('getLongPoll', () => {
      this.callMethod('messages.getLongPollServer', {}, (body) => {
        this.tempPoll     = JSON.parse(body)['response'];
        longPollMap.get('callLongPoll')();
      });
    });
    
    longPollMap.set('callLongPoll', () => {
    if(this.cap==false) {
    request({
        url: 'https://'+this.tempPoll['server'],
        qs: { 
          act:         'a_check',
          key:          this.tempPoll['key'].substr(0, (this.tempPoll['key'].length - 10)),
          ts:           this.tempPoll['ts'],
          wait:         25,
          mode:          2
        }
      }, (error, response, body) => {
    try { 
          body = JSON.parse(body);
          if('failed' in body)
            longPollMap.get('getLongPoll')();
          else {
     this.tempPoll['ts'] = body['ts'];
     }
           callback(body);
     longPollMap.get('callLongPoll')();
        } catch(e) {
          console.log('LONGPOLL: '+e);
          longPollMap.get('getLongPoll')();
        }
      });
    } else longPollMap.get('callLongPoll')();
    
    });
  
    longPollMap.get('getLongPoll')();
  }

As soon as cap becomes true, recursion should occur until it becomes false (another function performs). But the trouble is that the RangeError: Maximum call stack size exceeded error is returned and the application is restarted. How to do recursion?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question