1

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
};
  • 1. If you place console.log(wells) before const organoidObject = displaceCoordinates(wellName, orgObj, wells); 2. is **another function** marked as async ? – Svetoslav Petkov Sep 20 '22 at 15:37
  • there is another async function right before the const wells line. I have updated the post to show it. – Joan Krastanov Sep 20 '22 at 15:41
  • Code here looks totally fine. Please provide a [mcve]. – ggorlen Sep 20 '22 at 15:41
  • @SvetoslavPetkov `another function` would have to be async or the code wouldn't pass syntax checking. – ggorlen Sep 20 '22 at 15:43
  • @ggorlen i updated the code hopefully it gives you a better understanding of the flow now – Joan Krastanov Sep 20 '22 at 15:54
  • Thanks, but it still looks fine to me. I'm pretty sure the problem is somewhere other than where you assume it to be and so you've lost the issue in translation. Hence the importance of a [mcve]. – ggorlen Sep 20 '22 at 15:56
  • Well, clearly there's a problem with your code, not with TypeScript or JavaScript, so there's more debugging work to be done here. Generally, this issue arises from not awaiting a promise in the chain somewhere. The `forEach` and `map`s are [not very async-compatible](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) and often the cause of problems, but nothing happening in these loops was shown to be async, so something critical has been omitted. If you use `map` async you pretty much need to use `Promise.all` (or family). – ggorlen Sep 20 '22 at 15:59
  • @ggorlen well since I directly pass the variable to another function what could possibly change its value because I am not modifying it anywhere.. – Joan Krastanov Sep 20 '22 at 15:59
  • I would ask again to do console.log(wells), before calling: *const organoidObject = displaceCoordinates(wellName, orgObj, wells);* Debugger will also help. – Svetoslav Petkov Sep 20 '22 at 16:20
  • Good advice--step slowly forwards/backwards printing `wells` until you figure out where it's undefined. – ggorlen Sep 20 '22 at 16:41
  • For some reason when I added async in the foreach before organoid and async before displaceCoordinates it seemed to fix it, even though displaceCoordinates in not an async function – Joan Krastanov Sep 20 '22 at 16:57

0 Answers0