Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tsertkov/masynco

massive async operations 🚙
https://github.com/tsertkov/masynco

async limit mapreduce nodejs

Last synced: 23 days ago
JSON representation

massive async operations 🚙

Awesome Lists containing this project

README

        

# masynco

> massive async operations powered by [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)

Minimal async map/reduce helper with pluggable limit to handle massive amount of asynchronous operations in node and browser.

## Usage

```javascript
// const masynco = require('masynco') // cjs and mjs are supported
import masynco from 'masynco'

;(async () => {
// supports async and regular functions
const fn = async (num) => num + 1

// supports async array mapping
const a = await masynco([1, 2, 3], fn)
console.log(a) // [ 2, 3, 4 ]

// supports async object mapping
const o = await masynco({ 'k1': 'v1', 'k2': 'v2' }, fn)
console.log(o) // { k1: 'v11', k2: 'v21' }

// supports regular (non-async) functions also
const fnSync = (num) => num + 1
const o1 = await masynco({ 'k1': 'v1', 'k2': 'v2' }, fnSync)
console.log(o1) // { k1: 'v11', k2: 'v21' }
})()
```

Optionally `limit` function can be passed for concurrency control:

```javascript
import masynco from 'masynco'
import plimit from 'p-limit'

;(async () => {
const fn = async (num) => num + 1
const o = await masynco({ 'k1': 'v1', 'k2': 'v2' }, fn, plimit(3))
console.log(o) // { k1: 'v11', k2: 'v21' }
})()
```

Use in browser as JavaScript module:

```html

import masynco from './masynco.js'

;(async () => {
const fn = async (num) => num + 1
const o = await masynco({ 'k1': 'v1', 'k2': 'v2' }, fn)
console.log(o) // { k1: 'v11', k2: 'v21' }
})()

```

## API

### async masynco(input, fn, limit = null)

Returns a new Array or Object formed by applying a given function to each element of input.

#### iterable

Type: `object`, `array`

Input Array or Object to process. Function returns array when input is array and object otherwise.

#### fn

Type: `function (value, key)`

Function that is called for every element of input iterable. It receives original `value` and `key` and returns new value that is inserted into output iterable.

#### limit

Type: `function`

Optional limit function for concurrency control. See `p-limit`.

## Why?

Single interface for applying regular and async map function to every item of input iterable and returning new iterable with original keys and mapped values.

" It saves my time, Al