Socket
Socket
Sign inDemoInstall

fw

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fw

Tiny library for asynchronous control-flow in JavaScript


Version published
Weekly downloads
3K
increased by2.7%
Maintainers
1
Weekly downloads
 
Created
Source

fw Build Status NPM version

Stagebeta

About

fw is a tiny library (<200 SLOC) which helps with asynchronous control-flow management in JavaScript environments

It exploits the functional-style programming based on high-order functions and other common patterns. You could use it in conjunction with hu, for a better approach

Features

  • Common control-flow management (series, parallel...)
  • Runs in node and browsers
  • Simple and easy-to-use API
  • Tiny (<200 SLOC)
  • Dependency-free
  • Designed to be embedded in other libraries or applications

Installation

Node.js
$ npm install fw --save
Browser

Via Bower package manager

$ bower install fw

Or loading the script remotely (just for testing or development)

<script src="//rawgithub.com/h2non/fw/master/fw.js"></script>

Environments

It works properly in any ES5 compliant engine

  • Node.js
  • Chrome >= 5
  • Firefox >= 3
  • Safari >= 5
  • Opera >= 12
  • IE >= 9

API

var fw = require('fw')

series(tasks, callback)

Run the functions in the array in series, each one running once the previous function has completed

If any functions in the series pass an error to its callback, no more functions are run and callback is immediately called with the value of the error

Arguments
  • tasks - An array containing functions to run. Each function is passed a callback(err, result)
  • callback - Optional callback to run once all the functions have completed. This function gets an array of results containing all the result arguments passed to the task callbacks. undefined or null values will be omitted from results
fw.series([
  function (next) {
    setTimeout(function () {
      next(null, 1)
    }, 100)
  },
  function (next, result) {
    setTimeout(function () {
      next(null, 2)
    }, 100)
  }
], function (err, results) {
  console.log(err) // → undefined
  console.log(results) // → [1, 2]
})

parallel(tasks, callback)

Run the tasks array of functions in parallel, without waiting until the previous function has completed

Once the tasks have completed, the results are passed to the final callback as an array

arguments
  • tasks - An array containing functions to run. Each function is passed a callback(err, result)
  • callback - Optional callback to run once all the functions have completed. This function gets an array of results containing all the result arguments passed to the task callbacks. undefined or null values will be omitted from results
fw.parallel([
  function (done) {
    setTimeout(function () {
      done(null, 1)
    }, 100)
  },
  function (done, result) {
    setTimeout(function () {
      done(null, 2)
    }, 150)
  }
], function (err, results) {
  console.log(err) // → undefined
  console.log(results) // → [1, 2]
})

whilst(test, fn, callback)

Repeatedly call a function, while test returns true. Calls callback when stopped or an error occurs

arguments
  • test() - synchronous truth test to perform before each execution of fn.
  • fn(callback) - A function which is called each time test passes. The function is passed a callback(err), which must be called once it has completed with an optional err argument
  • callback(err) - A callback which is called after the test fails and repeated execution of fn has stopped
var count = 0

fw.whilst(
  function () {
    return count < 3
  },
  function (callback) {
    count++
    setTimeout(callback, 1000)
  },
  function (err) {
    // 3 seconds have passed
  }
)

Contributing

Wanna help? Cool! It will be really apreciated :)

You must add new test cases for any new feature or refactor you do, always following the same design/code patterns that already exist

Development

Only node.js is required for development

Clone/fork this repository

$ git clone https://github.com/h2non/fw.git && cd fw

Install dependencies

$ npm install

Compile code

$ make compile

Run tests

$ make test

Generate browser sources

$ make browser

License

MIT © Tomas Aparicio

Keywords

FAQs

Package last updated on 19 Apr 2014

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