simpleasync
Advanced tools
Comparing version
@@ -1,1 +0,28 @@ | ||
function Sequence() { } function createSequence() { return new Sequence(); } module.exports = createSequence; | ||
function Runner(fns, elsefn) { var k = 0; var l = fns.length; var data = null; this.run = function (initialdata) { data = initialdata; doStep(); } function doStep() { if (k >= l) return; var fn = fns[k++]; try { if (fn.length > 1) { fn(data, function (err, newdata) { if (err) { elsefn(err); return; } data = newdata; setImmediate(doStep); }); } else { data = fn(data); setImmediate(doStep); } } catch (err) { elsefn(err); return; } } } | ||
function Sequence() { | ||
var fns = []; | ||
var elsefn = function (err) { throw err; | ||
} | ||
this.then = function (fn) { | ||
fns.push(fn); | ||
return this; | ||
} | ||
this.else = function (fn) { | ||
elsefn = fn; | ||
return this; | ||
} | ||
this.run = function (data) { var runner = new Runner(fns, elsefn); setImmediate(runner.run(data)); | ||
return this; | ||
} | ||
} | ||
function createSequence() { | ||
return new Sequence(); | ||
} | ||
module.exports = createSequence; |
{ "name": "simpleasync" | ||
, "description": "Simple flow library to chain and run functions with asynchronous calls" | ||
, "keywords": [ "node", "asynchronous", "library" ] | ||
, "version": "0.0.1alpha" | ||
, "version": "0.0.1" | ||
, "author": "Angel 'Java' Lopez <webmaster@ajlopez.com> (http://www.ajlopez.com)" | ||
@@ -6,0 +6,0 @@ , "repository": { "type": "git", "url": "git://github.com/ajlopez/SimpleAsync.git" } |
@@ -20,4 +20,79 @@ # SimpleAsync | ||
``` | ||
[TBD] (see tests). | ||
Define and run steps with then: | ||
```js | ||
async() | ||
.then(function (data) { | ||
console.log(data); | ||
return data + 1; | ||
}) | ||
.then(function (data) { | ||
console.log(data); | ||
}) | ||
.run(10); | ||
``` | ||
Expected output | ||
``` | ||
10 | ||
11 | ||
``` | ||
Define and run steps with one step using a callback. Then, you can write async steps: | ||
```js | ||
async() | ||
.then(function (data, next) { | ||
console.log(data); | ||
next(null, data + 1); // callback(err, newdata); | ||
}) | ||
.then(function (data) { | ||
console.log(data); | ||
}) | ||
.run(10); | ||
``` | ||
Expected output | ||
``` | ||
10 | ||
11 | ||
``` | ||
Use else for catch errors: | ||
```js | ||
async() | ||
.then(function (data) { | ||
console.log(data); | ||
throw "Houston, we have a problem"; | ||
}) | ||
.else(function (err) { | ||
console.log('error:', err); | ||
}) | ||
.run(10); | ||
``` | ||
Expected output | ||
``` | ||
10 | ||
error: Houston, we have a problem | ||
``` | ||
Raise an error in an step with callback | ||
```js | ||
async() | ||
.then(function (data, next) { | ||
console.log(data); | ||
next("Houston, we have a problem", null); | ||
}) | ||
.else(function (err) { | ||
console.log('error:', err); | ||
}) | ||
.run(10); | ||
``` | ||
Expected output | ||
``` | ||
10 | ||
error: Houston, we have a problem | ||
``` | ||
## Development | ||
@@ -24,0 +99,0 @@ |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
8731
212.16%11
120%145
3525%128
141.51%0
-100%