Skip to content

Instantly share code, notes, and snippets.

@priyankatgit
Created August 15, 2022 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priyankatgit/a04ace9f04d691e007afd249a7f6313f to your computer and use it in GitHub Desktop.
Save priyankatgit/a04ace9f04d691e007afd249a7f6313f to your computer and use it in GitHub Desktop.
Promise Pool Showcase
const { PromisePool } = require("@supercharge/promise-pool");
const { products } = require("./products");
async function execute() {
const { results } = await PromisePool.for(products)
.withConcurrency(10)
.process(importProduct);
console.log(results);
}
async function importProduct(data, index) {
const result = await createProduct(data, index);
console.log(result);
return result;
}
async function createProduct(data, index) {
const dummyProcessDelay = data.rating * 100;
return new Promise((resolve) =>
setTimeout(() => {
resolve({ item: index, delay: dummyProcessDelay });
}, dummyProcessDelay)
);
}
execute();
@priyankatgit
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment