0

I am trying to run async.parallel inside an ES6 class like this:

public getConsole(req: Request, res: Response) {
    async.parallel({
        count: this.getDocumentCount,
        collectionSize: this.getCollectionMb
    }, 
    (err, results) => {
        res.json(results);
    })
}

but this is undefined. I can't do . . . this.getDocumentCount.bind(this) because this is still undefined in that context. I also can't do

    async.parallel({
        count: this.getDocumentCount,
        collectionSize: this.getCollectionMb
    }.bind(this), 
    (err, results) => {
        res.json(results);
    })
}

because typescript complains.

Cameron Sima
  • 5,086
  • 6
  • 28
  • 47
  • See "Common problem: Using object methods as callbacks / event handlers" in the duplicate. – Felix Kling Jun 21 '17 at 01:58
  • `this.getDocumentCount.bind(this)` should work fine if `getConsole` is properly defined in the class and `getConsole` is called correctly. `{ ... }.bind(this)` cannot work because `.bind` is a method on functions, not objects. – Felix Kling Jun 21 '17 at 01:59

0 Answers0