@basementuniverse/async
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Async

Async versions of common list functions.

Installation

npm install @basementuniverse/async

Usage

See docs for more information.

asyncForEach

import { asyncForEach } from '@basementuniverse/async';

await asyncForEach<string>(
  [
    'one',
    'two',
    'three',
  ],
  async (value: string) => {
    await fetch(`localhost:8080/${value}`);
  }
);

asyncMap

import { asyncMap } from '@basementuniverse/async';

const results = await asyncMap<string, number>(
  [
    '123',
    '456',
    '789',
  ],
  async (value: string) => {
    // asynchronous stuff here...
    return Number(value);
  }
);

/*
results: [123, 456, 789]

Note that the order of results might be different.
*/

asyncFilter

import { asyncFilter } from '@basementuniverse/async';

const results = await asyncFilter<string>(
  [
    'allowed1',
    'notAllowed2',
    'allowed3',
    'notAllowed4',
  ],
  async (value: string) => {
    // asynchronous stuff here...
    return value.startsWith('allowed');
  }
);

/*
results: ['allowed1', 'allowed2']

Note that the order of results might be different.
*/

asyncReduce

import { asyncReduce } from '@basementuniverse/async';

const result = await asyncReduce<string, number>(
  [
    '1',
    '2',
    '3',
  ],
  async (previous: number, current: string) => {
    // asynchronous stuff here...
    return previous + Number(current);
  },
  0
);

/*
result: 6
*/

Package Sidebar

Install

npm i @basementuniverse/async

Weekly Downloads

17

Version

1.0.0

License

MIT

Unpacked Size

9.1 kB

Total Files

7

Last publish

Collaborators

  • basementuniverse