$ cnpm install fw
fw is a tiny library which helps with asynchronous control-flow management in JavaScript environments
It exploits functional-style programming using higher-order functions and other common patterns. You could use it in conjunction with hu, for a better approach
$ npm install fw --save
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>
It works properly in any ES5 compliant engine
var fw = require('fw')
Run the functions in the array in series (sequentially). Each function will be executed only if its 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
callback(err, result)undefined or null values will be omitted from resultsfw.series([
function (next) {
setTimeout(function () {
next(null, 1)
}, 100)
},
function (next, result) {
setTimeout(function () {
next(null, result + 1)
}, 100)
}
], function (err, results) {
console.log(err) // → undefined
console.log(results) // → [1, 2]
})
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
callback(err, result)undefined or null values will be omitted from resultsfw.parallel([
function (done) {
setTimeout(function () {
done(null, 1)
}, 100)
},
function (done) {
setTimeout(function () {
done(null, 2)
}, 150)
}
], function (err, results) {
console.log(err) // → undefined
console.log(results) // → [1, 2]
})
Repeatedly call a function, while test returns true. Calls callback when stopped or an error occurs
callback(err), which must be called once it has completed with an optional err argumentfn has stoppedvar count = 0
fw.whilst(
function () {
return count < 3
},
function (callback) {
count++
setTimeout(callback, 1000)
},
function (err) {
// 3 seconds have passed
}
)
Alias: map, eachParallel, mapParallel
Applies the function iterator to each item in arr, in parallel. The iterator is called with an item from the list, and a callback for when it has finished
Note that since this function applies iterator to each item in parallel, there is no guarantee that the iterator functions will complete in order
var fs = require('fs')
var files = ['package.json', 'bower.json']
fw.each(files, fs.readFile, function (err, results) {
console.log(err) // → undefined
console.log(results) // → [Buffer, Buffer]
})
Alias: mapSeries
The same as each(), but only iterator is applied to
each item in the array in series
The next iterator is only called once the current one has completed (sequentially)
var fs = require('fs')
var files = ['package.json', 'bower.json']
fw.eachSeries(files, fs.readFile, function (err, results) {
console.log(err) // → undefined
console.log(results) // → [Buffer, Buffer]
})
Wanna help? Great! 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
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
MIT © Tomas Aparicio
Copyright 2013 - present © cnpmjs.org | Home |