Socket
Socket
Sign inDemoInstall

array-promise-filter

Package Overview
Dependencies
2
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    array-promise-filter

filter an array through async callback


Version published
Weekly downloads
49
increased by48.48%
Maintainers
1
Install size
179 kB
Created
Weekly downloads
 

Readme

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

Last updated on 27 Sep 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