Socket
Socket
Sign inDemoInstall

async-array-methods

Package Overview
Dependencies
7
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-array-methods

Async methods to operate on collections


Version published
Maintainers
1
Created

Changelog

Source

v1.0.0 (2015-10-10)

Readme

Source

async-array-methods

Async methods to operate on collections.

npm

version status dependencies devDependencies

Usage

Methods:

Callbacks

Callbacks can be made asynchronous by returning a promise, or appending a function argument, which will be called when the callback finishes.

Otherwise, the callback is treated as synchronous.

filter

Signature: filter(arr, fn, done)

filter(
  [1, 2, 3, 4],
  function (v) {
    return new Promise(function (rs) {
      process.nextTick(function () {
        rs(v % 2);
      });
    });
  },
  function (err, results) {
    console.log('promise:', results);
  }
);

map

Signature: map(arr, fn, done)

map(
  [1, 2, 3, 4],
  function (v, i, a, next) {
    process.nextTick(function () {
      next(null, v << 2);
    });
  },
  function (err, results) {
    console.log('async:', results);
  }
);

reduce

Signature: reduce(arr, fn, initial, done)

reduce(
  [1, 2, 3, 4],
  function (a, b, i, arr, next) {
    process.nextTick(function () {
      next(null, a + b);
    });
  },
  function (err, results) {
    console.log('async:', results);
  }
);

chain

Signature: chain(arr, callbacks, done)

var methods = require('async-array-methods')
methods.chain(
  [1, 2, 3, 4],
  [
    function (res) {
      return res.map(function (r) {
        return ++r
      })
    },
    [methods, 'filter', odd],
    function (res, next) {
      process.nextTick(function () {
        next(null, res.map(function (r) {
          return ++r
        }))
      })
    },
    [methods, 'map', plusplus],
    function (res) {
      return new Promise(function (rs) {
        process.nextTick(function () {
          rs(res.map(function (r) {
            return ++r
          }))
        })
      })
    },
    [methods, 'reduce', sum, 10],
  ],
  function (err, res) {
    console.log(err, res)
  }
)

function odd(v) {
  return v % 2;
}

function plusplus(v, i, a, next) {
  process.nextTick(function () {
    next(null, ++v);
  });
}

function sum(a, b) {
  return new Promise(function (rs) {
    process.nextTick(function () {
      rs(a + b);
    });
  });
}

Keywords

FAQs

Last updated on 10 Oct 2015

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