I have an object with an array property which I want to fill with the responses received from an asynchronous call. This call is being made by a method from a 3rd party library that receives a call back function, so I tried something like this:
class Foo {
constructor() {
this.myArray = [];
thirdPartyObject.thirdPartyMethod(
param1,
param2,
function(data){ //success callback
data.forEach(item => {
var myArrayEle = {
propertyA : item.propertyA,
propertyB : item.propertyB
};
this.myArray.push(myArrayEle);
})
},
function(data){ //error callback
throw "Error"
}
)
}
}
I get an error on this.SomeArray.push(myArrayEle);
saying "cannot read property myArray of undefined" though.
I don't want to simply do stuff with the response then let it go as I want to keep this array stored so I can do stuff with it depending on what the user does instead of having to make another call later on to get the same data.
Is there a way to do this inside the constructor or do I have to use promises somewhere else to do this?