plimit.js

Simple promise concurrency limiter
git clone git://git.finwo.net/lib/plimit.js
Log | Files | Refs | README

index.js (605B)


      1 (factory => {
      2   if ('object' === typeof module) {
      3     module.exports = factory();
      4   } else if ('object' === typeof window) {
      5     Object.assign(window, factory());
      6   } else {
      7     throw new Error("Neither 'module.exports' nor 'window' exist");
      8   }
      9 })(() => ({
     10   plimit(concurrency) {
     11     const queue = [];
     12     return {
     13       push(task) {
     14         queue.push(task);
     15         if (queue.length >= concurrency) {
     16           return queue.shift();
     17         } else {
     18           return Promise.resolve();
     19         }
     20       },
     21       async flush() {
     22         while(queue.length) await queue.shift();
     23       },
     24     };
     25   }
     26 }));