plimit.js

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

README.md (1216B)


      1 # @finwo/p-limit
      2 
      3 Simple promise concurrency limiter
      4 
      5 > **Please Note:** Due to the very limited scope of this module, I do not anticipate needing to make very many changes to it.  Expect long stretches of zero updates—that does not mean that the module is outdated.
      6 
      7 ## Installation
      8 
      9 ```sh
     10 npm install --save @finwo/p-limit
     11 ```
     12 
     13 ```js
     14 # node
     15 import { plimit } from '@finwo/p-limit';
     16 ```
     17 
     18 ```html
     19 <!-- browser -->
     20 <script src="/path/to/plimit/index.js"></script>
     21 <script>
     22   // use window.plimit
     23 </script>
     24 ```
     25 
     26 ## Usage
     27 
     28 The package assumes native promises are available
     29 
     30 This example shows iterating over accounts without loading all into memory
     31 
     32 ```js
     33 import { MoreThan } from 'typeorm';
     34 import { Account } from './model/account'; // typeorm model
     35 
     36 const runner    = plimit(4); // runner that allows 4 concurrent tasks
     37 let   accountId = '';
     38 
     39 while(true) {
     40   const account = await Account.findOne({ uuid: MoreThan(accountId) });
     41   if (!account) break;
     42   accountId = account.uuid;
     43 
     44   // resolves instantly it's queue is not full
     45   // waits for a previous task if it's queue is full
     46   await runner.push(async () => {
     47     // some lengthy process on the account
     48   });
     49 }
     50 
     51 // Process remainder
     52 await runner.flush();
     53 ```