1.0.5 • Published 2 years ago

@finwo/p-limit v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@finwo/p-limit

Simple promise concurrency limiter

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.

Installation

npm install --save @finwo/p-limit
# node
import { plimit } from '@finwo/p-limit';
<!-- browser -->
<script src="/path/to/plimit/index.js"></script>
<script>
  // use window.plimit
</script>

Usage

The package assumes native promises are available

This example shows iterating over accounts without loading all into memory

import { MoreThan } from 'typeorm';
import { Account } from './model/account'; // typeorm model

const runner    = plimit(4); // runner that allows 4 concurrent tasks
let   accountId = '';

while(true) {
  const account = await Account.findOne({ uuid: MoreThan(accountId) });
  if (!account) break;
  accountId = account.uuid;

  // resolves instantly it's queue is not full
  // waits for a previous task if it's queue is full
  await runner.push(async () => {
    // some lengthy process on the account
  });
}

// Process remainder
await runner.flush();
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago