0

I tried to use async/await then I put await on a function call but even it still not finished it already execute the next line.

async function listFreeAccounts() {
    let rnds = [];
    await gun.get('freeAccounts').map().once((v, k) => {
        // this will be called many times defending on the number of values exists.
        // then push it on the array.
        rnds.push(k);
    });

    // Premature returns here, making the rnds values empty.
    return rnds;
};

How I can make to wait to push all the values in rnds before returning it?

Thanks.

Edit

I am using gundb that's the gun.get(... code.

zer09
  • 1,507
  • 2
  • 28
  • 48
  • What does `once` return? – riv Jun 14 '18 at 08:19
  • that is a key value pair, if it found 10 items then it will call the callback 10 times – zer09 Jun 14 '18 at 08:21
  • `await` on a non-promise will return immediately, so if the library function doesn't return a promise, then you can't do it this way. – riv Jun 14 '18 at 08:23
  • 1
    You can only await a function that returns a promise. Does gun.get('freeAccounts').map().once() return a promise? If not, you'll need to wrap it in a function that does return a promise. – Quentin Jun 14 '18 at 08:23
  • ohh, I will try it and get back later – zer09 Jun 14 '18 at 08:26

0 Answers0