Await for a query to finish

What code syntax do I use to not proceed until a query has finished getting all rows? I have searched posts and think I have tried all suggestions but I know I'm missing something. The GetOrgsOfUser need finish before I scan through it to build the table. From what I have read I need it to execute in an async way but I have tried a bunch of different ways and can't get it to resolve before the subsequent code executes.

Here is my code:

var localUserID = 'XmMEhmVur9sX7tBDOrT7';
// const promise = async() => { await GetOrgsOfUser.trigger({
//   additionalScope: {
//     userID : localUserID
//   } 
// })};

const promise = Promise.resolve(await GetOrgsOfUser.trigger({
  additionalScope: {
    userID : localUserID
  } 
}));

var orgs =  GetOrgsOfUser.data.map(row => {
  return {"Name" : row.displayName,
          "Photo": row.photoURL
         };
});

return orgs

I know that is my issue because if I preview a second time it works.

1 Like

Had a similar issue the other day and solved it like this. Does that work for you?

(async () => {
  let localUserID = 'XXX';

  const result = await new Promise((resolve) => {
    GetOrgsOfUser.trigger({
      additionalScope: {
        userID: localUserId
      },
      onSuccess: (data) => {
        resolve(data);
      }
    });
  });

  return result;
})()

Thanks, but it didn't work. Same issue, first preview the map fails, second time, once the query has already resolved from the prior preview, it does show the result. Loom | Free Screen & Video Recording Software

Can you map over result instead of .data of the query and give me the output? (inside the async func)

I'm not sure what you mean. I tried more than a few ways to use result in the map both inside the async func and outside. I most likely was getting the syntax wrong since every way I tried produced errors. I do need the jsCode to return the list as I want to use it further in the function outside of the async function.

Does anyone else have any ideas?

Hey @George_B,

So what I meant was that instead of taking the data to map over from the queries response (.data), which I assume isn't available yet by runtime, you instead take the immediate success-callback response of the query.

I just setup a demo and it looks like you won't even need the async and could do something like:

let localUserID = 'XmMEhmVur9sX7tBDOrT7';
let results;

const orgs = await new Promise((resolve) => {
  GetOrgsOfUser.trigger({
    additionalScope: {
      userID : localUserID
    },
    onSuccess: (data) => {
      resolve(data);
    },
  });
});

orgs.map(row => {
  results = {
    name: row.displayName,
    photo: row.photoURL
	}
});

return results

Does that work for you?

3 Likes

It works! Thanks, you are a life saver.

The "org.map..." code was not necessary because orgs had the result I wanted for the table.

let localUserID = 'XmMEhmVur9sX7tBDOrT7';
let results;

const orgs = await new Promise((resolve) => {
  test_GetOrgsOfUser.trigger({
    additionalScope: {
      userID : localUserID
    },
    onSuccess: (data) => {
      resolve(data);
    },
  });
});

return orgs;

// orgs.map(row => {
//   results = {
//     name: row.displayName,
//     photo: row.photoURL
// 	}
// });

// return results

1 Like

Noice :v: Glad we worked it out