Socket
Book a DemoInstallSign in
Socket

array-promise-filter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-promise-filter

filter an array through async callback

Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

array-promise-filter

Filter an array through async callback. The results can be accessed from the returned promise.

res = filter(values, fn)

var filter = require('array-promise-filter');

values

Type: Array

The array to be filtered.

fn

Type: function

fn should do one of the following.

Respect synchronous signature

fn(val, index, arr)

var filter = require('..');
function isUpperCase(n) {
  return !n.split().some(function (c) {
    return c >= 'a';
  });
}
filter(['ab', 'CD', 'ef', 'GH'], isUpperCase)
  .then(function (res) {
    console.log(res); // ['CD', 'GH']
  })
  .catch(function (err) {
    console.log(err);
  });

Respect asynchronous signature

fn(val, index, arr, next)

next(err, keep) must be called when fn finishes.

var filter = require('..');
function isUpperCase(n, i, arr, next) {
  process.nextTick(function () {
    try {
      next(null, !n.split().some(function (c) {
        return c >= 'a';
      }));
    } catch (e) {
      next(e);
    }
  });
}
filter(['ab', 'CD', 'ef', 'GH'], isUpperCase)
  .then(function (res) {
    console.log(res); // ['CD', 'GH']
  })
  .catch(function (err) {
    console.log(err);
  });

Return a promise

var filter = require('..');
function isUpperCase(n) {
  return new Promise(function (rs, rj) {
    process.nextTick(function () {
      try {
        rs(!n.split().some(function (c) {
          return c >= 'a';
        }));
      } catch (e) {
        rj(e);
      }
    });
  });
}
filter(['ab', 'CD', 'ef', 'GH'], isUpperCase)
  .then(function (res) {
    console.log(res); // ['CD', 'GH']
  })
  .catch(function (err) {
    console.log(err);
  });

res

Type: Promise

Keywords

array

FAQs

Package last updated on 27 Sep 2015

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