I am making a function call from a useEffect
, the function is present in a different class. From that function I am calling another async function to make the ajax call after the promise is resolved, I am performing some operation on the data returned from the API call and then I am returning the data back to the useEffect
where the function is called in the first place. I am getting undefined in the useEffect
. Here is the code snippet
// In Main Class
let newObj=new ABC();
useEffect(()=>{
let flag= newObj.method1(url); //method present in another class.
setFlag(flag)
});
// Inside Class ABC
let flag;
method1(url){
this.method2(url).then(function(result) {
/*some operation */
flag=true; //set to true based on data
console.log(flag)//prints true
}
return flag // console log will print false here.
}
async method2(url){
let data=await axios.get(url);
return data;
}
The end result I am trying to get in the main class is true/false based on the API call, I want all the logic to be present in the class ABC.