Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

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

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc