async-counter

1.1.0 • Public • Published

npm version Build Status

async-counter

An asynchronous counter with a sync interface

Install

npm i --save async-counter

Usage

import asyncCounter from 'async-counter';
 
const counter = asyncCounter(2);
 
// Inside an async block
const logWhenFinished = async () => console.log(`Finished counting to ${await counter}!`);
 
// Somewhere else, async code
setTimeout(() => counter.count(), 500);
setTimeout(() => counter.count(), 1000);
// `counter` resolves the second time `counter.count` is called
 
logWhenFinished();
// Output after a second:
// Finished counting to 2!

API

const counter = asyncCounter(3, {
  onFinished: max => console.log(`Finished counting to ${max}!`),
  onCount: ({payload, max, current}) => console.log(`${payload.date.toString()}${current} of ${max} times`)
});
 
for (let i = 0; i < 3; i++) {
  setTimeout(() => counter.count({date: new Date()}), i * 1000);
}
// Output:
// Wed Feb 27 2019 04:40:05 GMT-0500 (Eastern Standard Time): 1 of 3 times
// Wed Feb 27 2019 04:40:06 GMT-0500 (Eastern Standard Time): 2 of 3 times
// Wed Feb 27 2019 04:40:07 GMT-0500 (Eastern Standard Time): 3 of 3 times
// Finished counting to 3!

License

MIT

Package Sidebar

Install

npm i async-counter

Weekly Downloads

28

Version

1.1.0

License

MIT

Unpacked Size

4.02 kB

Total Files

4

Last publish

Collaborators

  • keyvan-m-sadeghi