simpleasync
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -6,18 +6,13 @@ | ||
this.run = function (data, cbok, cbfail) { | ||
if (step) | ||
step.run(data, function (newdata) { | ||
var result = fn(newdata); | ||
cbok(result); | ||
}, function (err) { | ||
if (failfn) | ||
failfn(err); | ||
else if (cbfail) | ||
cbfail(err); | ||
else | ||
throw err; | ||
}); | ||
else { | ||
var result = fn(data); | ||
step.run(data, function (newdata) { | ||
var result = fn(newdata); | ||
cbok(result); | ||
} | ||
}, function (err) { | ||
if (failfn) | ||
failfn(err); | ||
else if (cbfail) | ||
cbfail(err); | ||
else | ||
throw err; | ||
}); | ||
} | ||
@@ -80,9 +75,3 @@ | ||
if (sfn.length > 1) { | ||
sfn(data, makecb(j, objcount, fnl)); | ||
} | ||
else { | ||
newdata[j] = sfn(data); | ||
objcount.count++; | ||
} | ||
sfn(data, makecb(j, objcount, fnl)); | ||
} | ||
@@ -92,3 +81,3 @@ | ||
data = newdata; | ||
setTimeout(doStep, 0); | ||
setImmediate(doStep); | ||
} | ||
@@ -112,3 +101,3 @@ | ||
data = newdata; | ||
setTimeout(doStep, 0); | ||
setImmediate(doStep); | ||
} | ||
@@ -130,3 +119,3 @@ } | ||
data = []; | ||
setTimeout(doStep, 0); | ||
setImmediate(doStep); | ||
return; | ||
@@ -162,3 +151,3 @@ } | ||
data = newdata; | ||
setTimeout(doStep, 0); | ||
setImmediate(doStep); | ||
@@ -176,3 +165,3 @@ return; | ||
data = newdata; | ||
setTimeout(doStep, 0); | ||
setImmediate(doStep); | ||
}); | ||
@@ -182,3 +171,3 @@ } | ||
data = fn(data); | ||
setTimeout(doStep, 0); | ||
setImmediate(doStep); | ||
} | ||
@@ -193,4 +182,8 @@ } | ||
function Sequence() { | ||
function Sequence(options) { | ||
var fns = []; | ||
options = options || {}; | ||
var data = null; | ||
var running = false; | ||
var self = this; | ||
@@ -200,10 +193,47 @@ var failfn = function (err) { | ||
} | ||
function launch() { | ||
if (running) | ||
return; | ||
if (options.autorun === false) | ||
return; | ||
run(data); | ||
} | ||
function run(dt) { | ||
data = dt; | ||
if (running) | ||
return this; | ||
running = true; | ||
setImmediate(function () { | ||
var runner = new Runner(fns, failfn); | ||
runner.run(data); | ||
}); | ||
return this; | ||
} | ||
this.data = function (dt) { | ||
data = dt; | ||
return this; | ||
} | ||
this.then = function (fn) { | ||
fns.push(fn); | ||
launch(); | ||
return this; | ||
} | ||
this.exec = function (fn) { | ||
this.then(function (data, next) { fn(next); }); | ||
return this; | ||
} | ||
this.map = function (fn) { | ||
fns.push({ map: fn }); | ||
launch(); | ||
return this; | ||
@@ -213,3 +243,3 @@ } | ||
this.do = function (fn) { | ||
if (fns.length == 0 || !Array.isArray(fns[fns.length-1])) | ||
if (fns.length === 0 || !Array.isArray(fns[fns.length-1])) | ||
fns.push([fn]); | ||
@@ -221,19 +251,23 @@ else | ||
this.fail = function (fn) { | ||
this.error = function (fn) { | ||
failfn = fn; | ||
launch(); | ||
return this; | ||
} | ||
this.run = function (data) { | ||
var runner = new Runner(fns, failfn); | ||
setTimeout(runner.run(data), 0); | ||
return this; | ||
this.fail = function (fn) { | ||
console.log('fail method is deprecated, use error method'); | ||
return this.error(fn); | ||
} | ||
this.run = function (dt) { | ||
console.log('run method is deprecated, use data method'); | ||
return this.data(dt); | ||
} | ||
} | ||
function createSequence() { | ||
return new Sequence(); | ||
function createSequence(options) { | ||
return new Sequence(options); | ||
} | ||
module.exports = createSequence; |
{ "name": "simpleasync" | ||
, "description": "Simple flow library to chain and run functions with asynchronous calls" | ||
, "keywords": [ "node", "asynchronous", "library" ] | ||
, "version": "0.0.7" | ||
, "version": "0.0.8" | ||
, "author": "Angel 'Java' Lopez <webmaster@ajlopez.com> (http://www.ajlopez.com)" | ||
@@ -10,3 +10,3 @@ , "repository": { "type": "git", "url": "git://github.com/ajlopez/SimpleAsync.git" } | ||
, "scripts": { | ||
"test": "simpleunit test" | ||
"test": "simpleunit ./test -r" | ||
} | ||
@@ -13,0 +13,0 @@ , "dependencies": { |
@@ -21,13 +21,15 @@ # SimpleAsync | ||
Define and run steps with then: | ||
In the following samples, `next` is a function `function (err, data)...` | ||
Define and run steps with data and then: | ||
```js | ||
async() | ||
.then(function (data) { | ||
.data(10) | ||
.then(function (data, next) { | ||
console.log(data); | ||
return data + 1; | ||
next(null, data + 1); | ||
}) | ||
.then(function (data) { | ||
.then(function (data, next) { | ||
console.log(data); | ||
}) | ||
.run(10); | ||
}); | ||
``` | ||
@@ -41,13 +43,13 @@ | ||
Define and run steps with one step using a callback. Then, you can write async steps: | ||
Use exec to inject asynchronous data: | ||
```js | ||
async() | ||
.exec(function (next) { next(null, 10); }) | ||
.then(function (data, next) { | ||
console.log(data); | ||
next(null, data + 1); // callback(err, newdata); | ||
next(null, data + 1); | ||
}) | ||
.then(function (data) { | ||
.then(function (data, next) { | ||
console.log(data); | ||
}) | ||
.run(10); | ||
}); | ||
``` | ||
@@ -61,13 +63,13 @@ | ||
Use `fail` for catch errors: | ||
Use `error` for catch errors: | ||
```js | ||
async() | ||
.then(function (data) { | ||
.data(10) | ||
.then(function (data, next) { | ||
console.log(data); | ||
throw "Houston, we have a problem"; | ||
}) | ||
.fail(function (err) { | ||
.error(function (err) { | ||
console.log('error:', err); | ||
}) | ||
.run(10); | ||
}); | ||
``` | ||
@@ -84,2 +86,3 @@ | ||
async() | ||
.data(10) | ||
.then(function (data, next) { | ||
@@ -89,6 +92,5 @@ console.log(data); | ||
}) | ||
.fail(function (err) { | ||
.error(function (err) { | ||
console.log('error:', err); | ||
}) | ||
.run(10); | ||
``` | ||
@@ -105,9 +107,9 @@ | ||
async() | ||
.do(function (data) { return data + 1; }) | ||
.do(function (data) { return data + 2; }) | ||
.do(function (data) { return data + 3; }) | ||
.data(1) | ||
.do(function (data, next) { next(null, data + 1); }) | ||
.do(function (data, next) { next(null, data + 2); }) | ||
.do(function (data, next) { next(null, data + 3); }) | ||
.then(function (data) { | ||
console.dir(data); | ||
}) | ||
.run(1); | ||
}); | ||
``` | ||
@@ -123,28 +125,10 @@ | ||
async() | ||
.do(function (data) { return data + 1; }) | ||
.do(function (data) { return data + 2; }) | ||
.do(function (data) { return data + 3; }) | ||
.map(function (data) { return data * 3; }) | ||
.then(function (data) { | ||
console.dir(data); | ||
}) | ||
.run(1); | ||
``` | ||
Expected output | ||
``` | ||
[ 6, 9, 12 ] | ||
``` | ||
`do` accepts function with callback: | ||
```js | ||
async() | ||
.data(1) | ||
.do(function (data, next) { next(null, data + 1); }) | ||
.do(function (data, next) { next(null, data + 2); }) | ||
.do(function (data, next) { next(null, data + 3); }) | ||
.map(function (data) { return data * 3; }) | ||
.map(function (data, next) { next(null, data * 3); }) | ||
.then(function (data) { | ||
console.dir(data); | ||
}) | ||
.run(1); | ||
}); | ||
``` | ||
@@ -156,27 +140,7 @@ | ||
``` | ||
The asynchronous functions accepted by a consecutive series of `do` are executed with concurrency. The data collected | ||
by the functions are assembled in an array, that is the data feeded into the next step. In the above example, the three | ||
async functions of `do` returned and array with | ||
``` | ||
[ 2, 3, 4 ] | ||
``` | ||
by the functions are assembled in an array, that is the data feeded into the next step. | ||
`map` accepts function with callback, too: | ||
```js | ||
async() | ||
.do(function (data, next) { next(null, data + 1); }) | ||
.do(function (data, next) { next(null, data + 2); }) | ||
.do(function (data, next) { next(null, data + 3); }) | ||
.map(function (data, next) { next(null, data * data); }) | ||
.then(function (data) { | ||
console.dir(data); | ||
}) | ||
.run(1); | ||
``` | ||
Expected output | ||
``` | ||
[ 4, 9, 16 ] | ||
``` | ||
## Development | ||
@@ -208,2 +172,3 @@ | ||
- 0.0.7: Published, improved do implementation, engine versions updated | ||
- 0.0.8: Published, fail and run deprecated, new data method, removing support for sync functions, autorun, exec method to inject async data | ||
@@ -210,0 +175,0 @@ ## License |
var async = require('..'); | ||
exports['do two functions'] = function (test) { | ||
exports['do two async functions'] = function (test) { | ||
test.async(); | ||
@@ -10,4 +10,5 @@ | ||
var seq = async() | ||
.do(function (data) { return data + 1; }) | ||
.do(function (data) { return data + 2; }) | ||
.data(1) | ||
.do(function (data, next) { next(null, data + 1); }) | ||
.do(function (data, next) { next(null, data + 2); }) | ||
.then(function (data) { | ||
@@ -23,6 +24,5 @@ test.ok(data); | ||
test.ok(seq); | ||
test.strictEqual(seq.run(1), seq); | ||
} | ||
exports['do two async functions'] = function (test) { | ||
exports['do two async functions with error'] = function (test) { | ||
test.async(); | ||
@@ -33,10 +33,11 @@ | ||
var seq = async() | ||
.data(1) | ||
.do(function (data, next) { next(null, data + 1); }) | ||
.do(function (data, next) { next(null, data + 2); }) | ||
.do(function (data, next) { next('error'); }) | ||
.then(function (data) { | ||
test.ok(data); | ||
test.ok(Array.isArray(data)); | ||
test.equal(data.length, 2); | ||
test.equal(data[0], 2); | ||
test.equal(data[1], 3); | ||
test.fail(); | ||
}) | ||
.error(function (err) { | ||
test.ok(err); | ||
test.equal(err, 'error'); | ||
test.done(); | ||
@@ -46,6 +47,5 @@ }); | ||
test.ok(seq); | ||
test.strictEqual(seq.run(1), seq); | ||
} | ||
exports['do two async functions with error'] = function (test) { | ||
exports['do two async functions without initial data'] = function (test) { | ||
test.async(); | ||
@@ -56,10 +56,10 @@ | ||
var seq = async() | ||
.do(function (data, next) { next(null, data + 1); }) | ||
.do(function (data, next) { next('error'); }) | ||
.do(function (data, next) { next(null, 1); }) | ||
.do(function (data, next) { next(null, 2); }) | ||
.then(function (data) { | ||
test.fail(); | ||
}) | ||
.fail(function (err) { | ||
test.ok(err); | ||
test.equal(err, 'error'); | ||
test.ok(data); | ||
test.ok(Array.isArray(data)); | ||
test.equal(data.length, 2); | ||
test.equal(data[0], 1); | ||
test.equal(data[1], 2); | ||
test.done(); | ||
@@ -69,4 +69,2 @@ }); | ||
test.ok(seq); | ||
test.strictEqual(seq.run(1), seq); | ||
} | ||
@@ -10,2 +10,3 @@ | ||
var seq = async() | ||
.data([1, 2, 3]) | ||
.map(function (item) { | ||
@@ -26,3 +27,2 @@ return item * item; | ||
test.ok(seq); | ||
test.strictEqual(seq.run([1,2,3]), seq); | ||
} | ||
@@ -36,2 +36,3 @@ | ||
var seq = async() | ||
.data([1, 2, 3]) | ||
.map(function (item, next) { | ||
@@ -52,3 +53,2 @@ next(null, item * item); | ||
test.ok(seq); | ||
test.strictEqual(seq.run([1,2,3]), seq); | ||
} | ||
@@ -62,2 +62,3 @@ | ||
var seq = async() | ||
.data([]) | ||
.map(function (item, next) { | ||
@@ -75,3 +76,2 @@ next(null, item * item); | ||
test.ok(seq); | ||
test.strictEqual(seq.run([]), seq); | ||
} | ||
@@ -85,2 +85,3 @@ | ||
var seq = async() | ||
.data([1, 2, 3]) | ||
.map(function (item, next) { | ||
@@ -95,3 +96,3 @@ if (item == 2) | ||
}) | ||
.fail(function (err) { | ||
.error(function (err) { | ||
test.equal(err, "error"); | ||
@@ -103,4 +104,3 @@ | ||
test.ok(seq); | ||
test.strictEqual(seq.run([1,2,3]), seq); | ||
} | ||
@@ -10,2 +10,3 @@ | ||
var seq = async() | ||
.data(10) | ||
.then(function (data) { | ||
@@ -19,3 +20,2 @@ total += data; | ||
test.ok(seq); | ||
test.strictEqual(seq.run(10), seq); | ||
} | ||
@@ -29,2 +29,3 @@ | ||
var seq = async() | ||
.data(6) | ||
.then(function (data) { | ||
@@ -42,3 +43,2 @@ total += data; | ||
test.ok(seq); | ||
test.strictEqual(seq.run(6), seq); | ||
} | ||
@@ -52,2 +52,3 @@ | ||
var seq = async() | ||
.data(6) | ||
.then(function (data, next) { | ||
@@ -67,3 +68,3 @@ total += data; | ||
test.ok(seq); | ||
test.strictEqual(seq.run(6), seq); | ||
} | ||
Sorry, the diff of this file is not supported yet
26670
687
15
180