-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.json
23 lines (23 loc) · 3.59 KB
/
package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"name": "step",
"version": "0.0.6",
"description": "A simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless.",
"engine": [
"node >=0.2.0"
],
"author": {
"name": "Tim Caswell",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "http://github.com/creationix/step.git"
},
"main": "lib/step",
"readme": "# Step\n\nA simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless.\n\n## How to install\n\nSimply copy or link the lib/step.js file into your `$HOME/.node_libraries` folder.\n\n## How to use\n\nThe step library exports a single function I call `Step`. It accepts any number of functions as arguments and runs them in serial order using the passed in `this` context as the callback to the next step.\n\n Step(\n function readSelf() {\n fs.readFile(__filename, this);\n },\n function capitalize(err, text) {\n if (err) throw err;\n return text.toUpperCase();\n },\n function showIt(err, newText) {\n if (err) throw err;\n console.log(newText);\n }\n );\n\nNotice that we pass in `this` as the callback to `fs.readFile`. When the file read completes, step will send the result as the arguments to the next function in the chain. Then in the `capitalize` function we're doing synchronous work so we can simple return the new value and Step will route it as if we called the callback.\n\nThe first parameter is reserved for errors since this is the node standard. Also any exceptions thrown are caught and passed as the first argument to the next function. As long as you don't nest callback functions inline your main functions this prevents there from ever being any uncaught exceptions. This is very important for long running node.JS servers since a single uncaught exception can bring the whole server down.\n\nAlso there is support for parallel actions:\n\n Step(\n // Loads two files in parallel\n function loadStuff() {\n fs.readFile(__filename, this.parallel());\n fs.readFile(\"/etc/passwd\", this.parallel());\n },\n // Show the result when done\n function showStuff(err, code, users) {\n if (err) throw err;\n console.log(code);\n console.log(users);\n }\n )\n\nHere we pass `this.parallel()` instead of `this` as the callback. It internally keeps track of the number of callbacks issued and preserves their order then giving the result to the next step after all have finished. If there is an error in any of the parallel actions, it will be passed as the first argument to the next step.\n\nAlso you can use group with a dynamic number of common tasks.\n\n Step(\n function readDir() {\n fs.readdir(__dirname, this);\n },\n function readFiles(err, results) {\n if (err) throw err;\n // Create a new group\n var group = this.group();\n results.forEach(function (filename) {\n if (/\\.js$/.test(filename)) {\n fs.readFile(__dirname + \"/\" + filename, 'utf8', group());\n }\n });\n },\n function showAll(err , files) {\n if (err) throw err;\n console.dir(files);\n }\n );\n\n*Note* that we both call `this.group()` and `group()`. The first reserves a slot in the parameters of the next step, then calling `group()` generates the individual callbacks and increments the internal counter.\n",
"_id": "[email protected]",
"dist": {
"shasum": "9998e10ba75e5a7fbe7d52f8f49cde584fe85638"
},
"_from": "step"
}