Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nue

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nue

An async control-flow library suited for the node event loop.

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.5K
decreased by-0.98%
Maintainers
1
Weekly downloads
 
Created
Source

nue — An async control-flow library

nue is an async control-flow library suited for the node event loop.

Installing

$ npm install nue

Example

var nue = require('nue');
var flow = nue.flow;
var fs = require('fs');

var myFlow = flow(
  function (file) {
    fs.readFile(file, 'utf-8', this.async());
  },
  function (data) {
    if (this.err) throw this.err;
    console.log(data);
  }
);

myFlow('file1');

API Overview

Core

  • flow

Serial

  • map
  • forEach
  • filter
  • every
  • some
  • queue

Parallel

API Detail

### flow([Function tasks...]) -> Function

Return a function which represents the control-flow.

Arguments

  • tasks: Optional. Tasks which are executed in series.

Context

this context of the each task has following properties.

  • next: Function. A function to execute a next task.
  • async: Function. A function to accept parameters for a next task and return a callback, which executes a next task.
  • end: Function. A function to execute a last task to end a control-flow. The first parameter is an error object.

In addition to the above ones, the context of the last task has a following property.

  • err: Object. An object represents an error which passed from the end function.

Example

var nue = require('nue');
var flow = nue.flow;
var fs = require('fs');

var myFlow = flow(
  function () {
    var results = [];
    fs.readFile('file1', this.async(results));
  },
  function (results, data) {
    results.push(data.length);
    fs.readFile('file2', this.async(results));
  },
  function (results, data) {
    results.push(data.length);
    this.next(results);
  },
  function (results) {
    if (this.err) throw this.err;
    console.log(results);
    this.next();
  }
);

myFlow();
### map(Function worker(arg)) -> Function

Return a function to process each value in series.

Arguments

  • worker: Required. Function. A function to process each value.
  • arg: Optional. Object. Each value passed from a previous task.

Context

this context of the worker has following properties.

  • next: Function. A function to process next value.
  • async: Function. A function to accept parameters for a next task and return a callback, which process next value.
  • end: Function. A function to execute a last task to end a control-flow. The first parameter is an error object.
  • isFirst: Boolean. Indicate whether the worker is handling the first value or not.
  • isLast: Boolean. Indicate whether the worker is handling the last value or not.
  • index: Number. An index of value.

Example

var nue = require('nue');
var flow = nue.flow;
var map = nue.map;
var fs = require('fs');

var myFlow = flow(
  function () {
    this.next('file1', 'file2', 'file3');
  },
  map(function (name) {
    var self = this;
    fs.readFile(name, function (err, data) {
      if (err) throw this.end(err);
      self.next(data.length);
    });
  }),
  function (results) {
    if (err) throw err;
    console.log(results);
    this.next();
  }
);

myFlow();
### parallel([Function tasks...]) -> Function

Return a function to execute tasks in parallel.

Arguments

  • tasks: Optional. Tasks which are executed in parallel.

Context

this context of each task has following properties.

  • next: Function. A function to complete a task and wait other tasks to complete.
  • async: Function. A function to accept parameters for a next task and return a callback, which complete a task and wait other tasks to complete.
  • end: Function. A function to execute a last task to end a control-flow. The first parameter is an error object.

Example

var nue = require('nue');
var flow = nue.flow;
var parallel = nue.parallel;
var fs = require('fs');

var myFlow = flow(
  function () {
    this.next('file1', 'file2');
  },
  parallel(
    function (name) {
      var self = this;
      fs.readFile(name, function (err, data) {
        if (err) this.end(err);
        self.next(data.length);
      });
    },
    function (path) {
      var self = this;
      fs.stat(path, function (err, stats) {
        if (err) this.end(err);
        self.next(stats.isFile());
      });
    }
  ),
  function (results) {
    if (this.err) throw this.err;
    console.log(results);
    this.next();
  }
);

myFlow();
### parallelMap(Function worker(arg)) -> Function

Return a function to process each value in parallel.

Arguments

  • worker: Optional. Function. A function to process each value.
  • arg: Optional. Object. Each value passed from a previous task.

Context

this context of the worker has following properties.

  • next: Function. A function to complete a processing and wait other processing to complete.
  • async: Function. A function to accept parameters for a next task and return a callback, which complete a processing and wait other processing to complete.
  • end: Function. A function to execute a last task to end a control-flow. The first parameter is an error object.

Example

var nue = require('nue');
var flow = nue.flow;
var parallelMap = nue.parallelMap;
var fs = require('fs');

var myFlow = flow(
  function () {
    this.next('file1', 'file2');
  },
  parallelMap(function (name) {
    var self = this;
    fs.readFile(name, function (err, data) {
      if (err) this.end(err);
      self.next(data.length);
    });
  }),
  function (results) {
    if (this.err) throw this.err;
    console.log(results);
    this.next();
  }
);

myFlow();

Keywords

FAQs

Package last updated on 15 Feb 2012

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