sindresorhus / p-limit

Run multiple promise-returning & async functions with limited concurrency
MIT License
2.06k stars 106 forks source link

Feature request: key based p-limit. #5

Closed ozomer closed 6 years ago

ozomer commented 6 years ago

This should probably be implemented in a separated repository. I am looking for a package that will support something like:

const pKeyLimit = require('p-key-limit');
const limiter = pKeyLimit(1); // concurrency is 1
limit('k1', () => taskA()).then(result => console.log(`Task A Done: ${result}`));
limit('k2', () => taskB()).then(result => console.log(`Task B Done: ${result}`)); // different key (k2) - will run in parallel to taskA
limit('k1', () => taskC()).then(result => console.log(`Task C Done: ${result}`)); // same key (k1) - will start executing after taskA has finished (recall that concurrency is 1).

Obviously, this could be implemented by maintaining a Map of keys to p-limits. However, there should be some way to clean the map from p-limits that have finished (maybe by wrapping the function that is passed to the internal p-limit).

I will be happy to hear if something like that was already implemented!

ozomer commented 6 years ago

Implemented myself: https://www.npmjs.com/package/p-key-limit .