1

I have the following situation. I have a class. This class has an async method. Inside this async method, I have something like this :

async AsyncMethodInsideClass(){
const resquest1 = await fecth(...);
 const request2 = await (request1.json())
for(const object of request2.objects){
          if(object.name === "Founded"
          this.newObject = object
}
}

Im trying to initialize this newObject member inside this async method. This async method is called in my class constructor.

Constructor(){
AsyncMethodInsideClass()
processNewObject(this.newObject)
}

However the processNewObject function is always being called before the AsyncMethodClass actually initializes the object, which leads to me using an undefined object inside the processNewObject. How should I solve this?

I've tried calling using await in another async function. I'm tried using then, but I really don't know the sintax to solve this. Would you please help me?

Icaro Amorim
  • 385
  • 1
  • 3
  • 13
  • off course the object is always founded, I've tested tha already. – Icaro Amorim Nov 21 '21 at 22:12
  • "*This async method is called in my class constructor.*" - well, don't. Do the fetching outside of the constructor or an instance method (i.e. a plain function or static method), then *pass* the founded `newObject` as an argument to the constructor. – Bergi Nov 21 '21 at 22:18

0 Answers0