Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a loop? #205

Open
robross0606 opened this issue Jul 26, 2019 · 1 comment
Open

Use a loop? #205

robross0606 opened this issue Jul 26, 2019 · 1 comment

Comments

@robross0606
Copy link

robross0606 commented Jul 26, 2019

I am absolutely baffled on how to use this client in a do..while loop. It appears the client does an async call, but I cannot seem to get it to await response before continuing.

async function test() {
  let doMore = true;
  do {
    let endpoint = `${share_api}/test/search/scopes/conceptId?start=${start}&pageSize=${pageSize}`;
    console.log(`Retrieving from ${endpoint}...`);
    let oldStart = start;
    await client
      .get(endpoint, args, (data, response) => {
        let values = data.values;
        let total = data.total;
        //   values.forEach(value => {
        //     console.log(value);
        //   });
        console.log(`Received ${values.length} result(s).`);
        doMore = data.hasMore;
        start += pageSize;
      })
      .on("error", function(err) {
        console.log("Something went wrong on the request", err.request.options);
        doMore = false;
      });
    doMore = oldStart < start;
    console.log("Got here.");
  } while (doMore);
}
@JL102
Copy link
Contributor

JL102 commented Jan 25, 2022

Try "promisifying" your retrieval and callback logic: https://javascript.info/promisify

Pseudo code:

async function test() {
  while(condition) {
    try {
      await promisify();
    } catch {..}
  }
}

function promisify() {
  return new Promise((resolve, reject) => {
    client.get(params, (data, response) => {
      // do stuff
      resolve();
    }).on('err', reject);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants