I have an async function that returns an array and when I call it directly it returns the array as expected but when I want to pass that array to another function, the other function receives undefined.
The second function is in a separate file but that shouldn't make a difference. I considered moving the getWells function to the displaceCoordinates.ts file and calling it there but since the displaceCoordinates function is called hundreds of times, it slows down the application by a considerable margin to the point where it takes at least a minute to load every object, practically making it unusable.
getOrganoids.ts file
const getWells = async (): Promise<IAreasRelativePosition[]> => {
const resp = await Omni3v1ExperimentsRepository.getAreasPositionsRelative(
"674fd1d5-889d-4170-8763-5b51f0634f1f",
time
);
return resp.data;
};
export const getOrganoids = async (): Promise<IOrganoid[]> => {
const dataArr = await fetchScanData(
`https://stororganoidtest.blob.core.windows.net/results/${projectId}/${experimentId}/${timeframe}/${wellName}.json`
);
const wells = (await getWells().catch(err =>
console.log(err)
)) as unknown as IAreasRelativePosition[];
console.log(wells); // returns response from the function as expected
const organoids: IOrganoid[] = [];
if (dataArr.length > 1) {
try {
dataArr.forEach(organoid => {
const newPoints = organoid.points.map(Object.values) as [
[number, number]
];
const orgObj: IOrganoid = {object};
const organoidObject = displaceCoordinates(wellName, orgObj, wells);
});
} catch (err) {
console.log(err);
}
}
return organoids;
};
displaceCoordinates.ts file
export const displaceCoordinates = (
wellName: string,
organoid: IOrganoid,
wells: IAreasRelativePosition[]
): IOrganoid => {
console.log(wells) // undefined
const wellArr = wells;
console.log(wellArr) // still undefined
};