You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

async-ray

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-ray

Allow perform filter, find, forEach, map and reduce on array using Async callback

2.0.2
Source
npmnpm
Version published
Weekly downloads
385
-68.95%
Maintainers
1
Weekly downloads
 
Created
Source

Async-Ray

Purpose of this package is to provide async/await callbacks forfilter, find, forEach, map, reduce methods in Array.

Basic usage

const { AsyncRay } = require('async-ray');

.aFilter

.aFilter(async callback(element[, index[, array]]))
async function dummy(element, needle) {
  return Promise.resolve(element > needle);
}

const inputArray = [1, 2, 3, 4];

// Call filter method
const filterArray = await AsyncRay(inputArray).aFilter(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i, 2);
  }
);

console.log(filterArray);
// Output is [3, 4]

.aFind

.aFind(async callback(element[, index[, array]]))

Find will return the found value or undefined

async function dummy(element, needle) {
  return Promise.resolve(element === needle);
}

const inputArray = [1, 2, 3, 4];

// Call Find method
const outputElement = await AsyncRay(inputArray).aFind(
  async (i, index, collection) => {
    return await dummy(i, 2);
  }
);

console.log('Output is ', outputElement);
// Output is 2

.aForEach

.aForEach(async callback(element[, index[, array]]))
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [1, 2, 3, 4];
const outputArray = [];

// Call ForEach method
await AsyncRay(inputArray).aForEach(async (i, index, collection) => {
  outputArray.push(await dummy(i));
});

console.log('Output is ', outputArray);
// Output is [1, 2, 3, 4]

.aMap

.aMap(async callback(element[, index[, array]]))
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [1, 2, 3, 4];

// Call Map method
const mappedArray = await AsyncRay(inputArray).aMap(
  async (i, index, collection) => {
    // Dummy async function
    return await dummy(i);
  }
);
console.log(mappedArray);
// Output is [1, 2, 3, 4]

.aReduce

.aReduce(async callback(accumulator, element[, index[, array]]), [initialValueOptional])
async function dummy(element) {
  return Promise.resolve(element);
}

const inputArray = [10, 20, 30, 40];

// Call reduce method
const output = await AsyncRay(inputArray).aReduce(
  async (acc, i, index, collection) => {
    return acc + (await dummy(i));
  },
  1
);

console.log('Output is ', output);
// Output is 101

Chaining methods

Please note that, .aFilter, .aFind, .aForEach, .aMap and aReduce cannot be chained with each other.

Eg:

// Below code will throw an error
AsyncRay([1,2,3]).aMap(...).aReduce(...)

But .aFilter, .aFind, .aForEach, .aMap and aReduce can be chained with other Array methods.

sample 1 - aMap and filter

async function dummy(ele) {
  return Promise.resolve(ele);
}

const inputArray = [1, 2, 3, 4];

const chainedValue = (await AsyncRay(inputArray).aMap(
  async (ele) => await dummy(ele * 10)
)).filter((ele) => ele > 20);

console.log('Output is ', chainedValue);
// Output is [30, 40]

sample 2 - aMap and find

async function dummy(ele) {
  return Promise.resolve(ele);
}

const inputArray = [1, 2, 3, 4];

const chainedValue = (await AsyncRay(inputArray).aMap(
  async (ele) => await dummy(ele * 10)
)).find((ele) => ele === 20);

console.log('Output is ', chainedValue);
// Output is 20

sample 3 - aMap and reduce

async function dummy(ele) {
  return Promise.resolve(ele);
}

const inputArray = [1, 2, 3, 4];

const chainedValue = (await AsyncRay(inputArray).aMap(
  async (ele) => await dummy(ele * 10)
)).reduce((acc, ele) => acc + ele), 1);

console.log('Output is ', chainedValue);
// Output is 101

Keywords

Array

FAQs

Package last updated on 17 Oct 2018

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.