1

In a typescript class, I'm trying to invoke an async member method. This is failing, as the method as invoked no longer seems to have access to the this property - it shows as undefined.

I'm hoping there's a way to explicitly bind the method to the class, or to pass some context in with my collection members when invoking asynchronously.

Here's some code for context:

class SomethingService implements SomethingServiceInterface {
    private MAX_ASYNC_THINGS = 10;

    constructor(@inject("SomethingRepository") private somethingRepository,
                @inject("OtherThingRepository") private otherThingRepository) {
        // setup, get logger, etc
    }

    public entryPoint(): Promise<Thing[]> {
        return this.getThings()
            .then((things: Thing[]) => {
                return async.mapLimit(things, this.MAX_ASYNC_THINGS, this.checkupThings, (err, things) => {
                    if (!err) return Promise.all(things);
                    else throw err; 
                });
            });
    }

    private checkupThings(thing: Thing): Promise<Thing> {
        return this.otherThingRepository.getAllThings()
            .then((otherThings) => {
               // business logic checking Thing against OtherThings or w/e
            });
    }
}

I get this behavior whether I wrap the reference to this.checkupThings in the async.mapLimit call in asyncify or not, or whether checkupThings is defined as private async checkupThings.

a p
  • 3,098
  • 2
  • 24
  • 46

0 Answers0