JSPM

async-array-methods

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 57
  • Score
    100M100P100Q72391F
  • License MIT

Async methods to operate on collections

Package Exports

  • async-array-methods

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (async-array-methods) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

fn is called with elements in parallel. If you want to run fn in sequence, use forEach instead.

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)
  }
)

forEach

fn is called with elements in sequence. If you want to run fn in parallel, use map instead.

Signature: forEach(arr, fn, done)

var count = 0

forEach(
  [1, 2, 3, 4],
  function (v, i, _, next) {
    process.nextTick(function () {
      console.log(count++ === i)
      next(null, v << 2)
    })
  },
  function (err, results) {
    console.log(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)
    })
  })
}