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

Make greenlet working with a pool of Promises #43

Open
empav opened this issue Jun 24, 2019 · 2 comments
Open

Make greenlet working with a pool of Promises #43

empav opened this issue Jun 24, 2019 · 2 comments
Labels
question Further information is requested

Comments

@empav
Copy link

empav commented Jun 24, 2019

Hi guys, maybe I'm wrong and need to give it a better look at service workers but what I'm trying to accomplish is the following:

1- I'd like to pass to greenlet an array of promises like the Promise.all(promises) return.
2- Await for them inside the greenlet function.
3- returning the result to the main app.

This is my code:

          try {
            const promises = validationAuto(chunk, workarea, criteria, test_groups); 
            if (window.Worker) {
              validationAutoWorkerized(promises);
            } else {
              await promises;
            }
          } catch (error) {
            console.log(error);
          }
const validationAuto = (chunk, workarea, criteria, test_groups) => {
  const validationPromises = chunk.map(id => {
    return new Promise((resolve, reject) => {
      postWorkareaValidationAuto(workarea, id, criteria, test_groups)
        .then(response => resolve(response))
        .catch(error => reject(error));
    });
  });
  return Promise.all(validationPromises);
};
const validationAutoWorkerized = greenlet(async promises => {
  const data = await promises;
  console.log(data);
  return data;
});

Any tip to accomplish it with greenlet? Thanks.

@developit
Copy link
Owner

developit commented Oct 1, 2019

Hi @epavan - you can't pass Promises to greenlet, because the code that generates the work those promises represent would still just be running on the main thread.

It's likely you're looking for something more like workerize-loader, which lets you import a module as a Worker:

// validation-auto.js
async function validationAuto(chunk, workarea, criteria, test_groups) {
  let validate;
  if (window.Worker) {
    // this special import runs the module and its dependencies in a worker:
    validate = (await import('workerize-loader!./validation-worker')).validate;
  }
  else {
    // if there's no Workers support, we'll just run it all on the main thread:
    validate = (await import('./validation-worker')).validate;
  }
  return validate(chunk, workarea, criteria, test_groups);
}
// validation-worker.js
export function validate(chunk, workarea, criteria, test_groups) {
  const validationPromises = chunk.map(id => {
    return new Promise((resolve, reject) => {
      postWorkareaValidationAuto(workarea, id, criteria, test_groups)
        .then(response => resolve(response))
        .catch(error => reject(error));
    });
  });
  return Promise.all(validationPromises);
}

The key with the above is that the code to create the Promises is itself moved into a Worker.

@developit developit added discussion question Further information is requested and removed discussion labels Oct 1, 2019
@Purecaesar
Copy link

Purecaesar commented Nov 9, 2019

@epavan if i understand your problem right. You can use it just like that, because if u are gonna make promisess you have to make it inside the worker.

const validationAutoWorkerized = greenlet(async (chunk, workarea, criteria, test_groups) => {
 const validationPromises = chunk.map(id => {
    return new Promise((resolve, reject) => {
      postWorkareaValidationAuto(workarea, id, criteria, test_groups)
        .then(response => resolve(response))
        .catch(error => reject(error));
    });
  });

  const data = await Promise.all(validationPromises);
  console.log(data);
  return data;
});

validationAutoWorkerized(chunk, workarea, criteria, test_groups);

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

No branches or pull requests

3 participants