Socket
Socket
Sign inDemoInstall

@wildpeaks/async

Package Overview
Dependencies
2
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @wildpeaks/async

Async loops for ES2017


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
16.5 kB
Created
Weekly downloads
 

Readme

Source

package-async

Loops for async functions.

Installation:

npm install @wildpeaks/async

asyncArrayMap

Similar to Array.map, except the mapper function should return a Promise, and it can optionally limit the number of Promises running at once (e.g. useful for calling a rate-limited API for example).

Syntax:

type CallbackType = (value: any, index: number, array: any[]) => Promise;

function asyncArrayMap(
	array: any[],
	callback: CallbackType,
	limit: number
): Promise<any[]>

Parameters:

  • array: Arbitrary list of values to process
  • callback: Async function that produces an element of the new Array
  • limit: Number of promises that can run at once (0 = no limit, 1 = only one at a time, 2 = two at once maximum, etc…)

Examples

Process all values in parallel (default, limit 0):

const {asyncArrayMap} = require('@wildpeaks/async');

const words = ['Aaa', 'Bbb', 'Ccc'];
const translations = await asyncArrayMap(words, async word => {
	const translated = await translate(word);
	return word + ' translates to ' + translated;
});

Only one at the time, one after the other (limit 1):

async function mapWord(word){
	const translated = await translate(word);
	return word + ' translates to ' + translated;
}

const {asyncArrayMap} = require('@wildpeaks/async');
const translations = await asyncArrayMap(['Aaa', 'Bbb', 'Ccc'], mapWord, 1);

Up to 5 at the same time in parallel (limit 5):

async function mapWord(word){
	const translated = await translate(word);
	return word + ' translates to ' + translated;
}

const {asyncArrayMap} = require('@wildpeaks/async');
const translations = await asyncArrayMap(['Aaa', 'Bbb', 'Ccc'], mapWord, 5);

Keywords

FAQs

Last updated on 07 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc