0

I've got a library that makes api calls to a number of sub-api's. If I call a function directly all works great:

let result = await apiLib.subApi1.getFoo();

But if I call by reference, I seem to lose the class context (i.e. 'this' is undefined within the class)

const endPoint = 'Foo';
let apiLibCall = apiLib.subApi1['get' + endPoint];
let result = await apiLibCall();

Is it possibly because of how 'await' works? Or am I calling by reference incorrectly?

BruceM
  • 1,287
  • 14
  • 33
  • 1
    Either call it directly `await apiLib.subApi1['get' + endPoint]()`. Or if you want to assign it to a variable, you need to `bind` the correct `this` like : `let apiLibCall = apiLib.subApi1['get' + endPoint].bind(apiLib.subApi1)`. Please check the duplicates on how `this` keyword works. – adiga Feb 01 '20 at 11:00
  • Thanks, I tried to check for duplicates, but they do indeed answer my question – BruceM Feb 01 '20 at 13:37

0 Answers0