async-harmony
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "async-harmony", | ||
"repo": "simov/async-harmony", | ||
"description": "Asynchronous Harmony", | ||
"version": "1.0.0", | ||
"keywords": ["async", "harmony", "asynchronous", "sequential", "parallel"], | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
}, | ||
"main": "lib/async-harmony.js", | ||
"scripts": ["lib/async-harmony.js"], | ||
"license": "MIT" | ||
"name": "async-harmony", | ||
"repo": "simov/async-harmony", | ||
"description": "Asynchronous Harmony", | ||
"version": "1.0.0", | ||
"keywords": ["async", "harmony", "asynchronous", "sequential", "parallel"], | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
}, | ||
"main": "lib/async-harmony.js", | ||
"scripts": ["lib/async-harmony.js"], | ||
"license": "MIT" | ||
} |
(function (name, root, factory) { | ||
if (typeof exports == 'object') | ||
module.exports = factory(); | ||
else if (typeof define == 'function' && define.amd) | ||
define(factory); | ||
else | ||
root[name] = factory(); | ||
if (typeof exports == 'object') | ||
module.exports = factory(); | ||
else if (typeof define == 'function' && define.amd) | ||
define(factory); | ||
else | ||
root[name] = factory(); | ||
@@ -14,19 +14,25 @@ }('async', this, function () { | ||
function check (errors) { | ||
return Object.keys(errors).length ? errors : null; | ||
} | ||
function series (jobs, done, kill=true) { | ||
var args = {}, results = {}, errors = {}; | ||
var args = {}, results = {}, errors = {}; | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
for (let job in jobs) { | ||
yield jobs[job](args, (err, result) => { | ||
if (kill && err) return done(err); | ||
if (err) errors[job] = err; | ||
args[job] = results[job] = result||null; | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
} | ||
done((Object.keys(errors).length ? errors : null), results); | ||
} | ||
function* iterator () { | ||
for (let job in jobs) { | ||
yield jobs[job](args, (err, result, quit) => { | ||
if (kill && err) return done(err); | ||
if (err) errors[job] = err; | ||
args[job] = results[job] = result||null; | ||
if (quit) return done(check(errors), results, quit); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
} | ||
done(check(errors), results); | ||
} | ||
} | ||
@@ -36,18 +42,19 @@ | ||
function eachSeries (items, job, done, kill=true) { | ||
var results = [], errors = []; | ||
var results = [], errors = []; | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
for (let item of items) { | ||
yield job(item, (err, result) => { | ||
if (kill && err) return done(err); | ||
if (err) errors.push(err); | ||
results.push(result||null); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
} | ||
done((errors.length ? errors : null), results); | ||
} | ||
function* iterator () { | ||
for (let item of items) { | ||
yield job(item, (err, result, quit) => { | ||
if (kill && err) return done(err); | ||
if (err) errors.push(err); | ||
results.push(result||null); | ||
if (quit) return done(check(errors), results, quit); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
} | ||
done(check(errors), results); | ||
} | ||
} | ||
@@ -57,27 +64,33 @@ | ||
function each (items, job, done, kill=false) { | ||
var results = [], errors = []; | ||
var total = items.length, finished = 0; | ||
var results = [], errors = []; | ||
var total = items.length, completed = 0; | ||
var terminated = false; | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
for (let item of items) { | ||
yield (() => { | ||
job(item, (err, result) => { | ||
if (total == -1) return; | ||
if (kill && err) { | ||
total = -1; | ||
return done(err); | ||
} | ||
if (err) errors.push(err); | ||
results.push(result||null); | ||
function* iterator () { | ||
for (let item of items) { | ||
yield (() => { | ||
job(item, (err, result, quit) => { | ||
if (terminated) return; | ||
if (kill && err) { | ||
terminated = true; | ||
return done(err); | ||
} | ||
if (err) errors.push(err); | ||
results.push(result||null); | ||
if (total == ++finished) | ||
done((errors.length ? errors : null), results); | ||
}); | ||
setTimeout(() => it.next(), 0); | ||
})(); | ||
} | ||
} | ||
if (quit) { | ||
terminated = true; | ||
return done(check(errors), results, quit); | ||
} | ||
if (total == ++completed) | ||
done(check(errors), results); | ||
}); | ||
setTimeout(() => it.next(), 0); | ||
})(); | ||
} | ||
} | ||
} | ||
@@ -87,36 +100,43 @@ | ||
function eachLimit (limit, items, job, done, kill=false) { | ||
var results = [], errors = []; | ||
var total = items.length, finished = 0; | ||
var index = 0, quota = limit; | ||
var results = [], errors = []; | ||
var total = items.length; | ||
var completed = 0, started = 0, running = 0; | ||
var terminated = false; | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
yield (() => { | ||
while (quota) { | ||
var item = items[index++]; | ||
quota--; | ||
function* iterator () { | ||
yield (() => { | ||
while (running < limit && started < total) { | ||
started++; | ||
running++; | ||
if (index > total) continue; | ||
job(items[started-1], (err, result, quit) => { | ||
if (terminated) return; | ||
if (kill && err) { | ||
terminated = true; | ||
return done(err); | ||
} | ||
job(item, (err, result) => { | ||
if (total == -1) return; | ||
if (kill && err) { | ||
total = -1; | ||
return done(err); | ||
} | ||
if (err) errors.push(err); | ||
results.push(result||null); | ||
if (err) errors.push(err); | ||
results.push(result||null); | ||
if (total == ++finished) | ||
return done((errors.length ? errors : null), results); | ||
if (quit) { | ||
terminated = true; | ||
return done(check(errors), results, quit); | ||
} | ||
quota++; | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
} | ||
})(); | ||
yield* iterator(); | ||
} | ||
completed++; | ||
running--; | ||
if (completed >= total) | ||
return done(check(errors), results); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
} | ||
})(); | ||
yield* iterator(); | ||
} | ||
} | ||
@@ -126,27 +146,33 @@ | ||
function parallel (jobs, done, kill=false) { | ||
var results = {}, errors = {}; | ||
var total = Object.keys(jobs).length, finished = 0; | ||
var results = {}, errors = {}; | ||
var total = Object.keys(jobs).length, completed = 0; | ||
var terminated = false; | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
for (let job in jobs) { | ||
yield (() => { | ||
jobs[job](((job) => ((err, result) => { | ||
if (total == -1) return; | ||
if (kill && err) { | ||
total = -1; | ||
return done(err); | ||
} | ||
if (err) errors[job] = err; | ||
results[job] = result||null; | ||
if (total == ++finished) | ||
done((Object.keys(errors).length ? errors : null), results); | ||
}))(job)); | ||
setTimeout(() => it.next(), 0); | ||
})(); | ||
} | ||
} | ||
function* iterator () { | ||
for (let job in jobs) { | ||
yield (() => { | ||
jobs[job](((job) => ((err, result, quit) => { | ||
if (terminated) return; | ||
if (kill && err) { | ||
terminated = true; | ||
return done(err); | ||
} | ||
if (err) errors[job] = err; | ||
results[job] = result||null; | ||
if (quit) { | ||
terminated = true; | ||
return done(check(errors), results, quit); | ||
} | ||
if (total == ++completed) | ||
done(check(errors), results); | ||
}))(job)); | ||
setTimeout(() => it.next(), 0); | ||
})(); | ||
} | ||
} | ||
} | ||
@@ -156,36 +182,44 @@ | ||
function parallelLimit (limit, jobs, done, kill=false) { | ||
var results = {}, errors = {}; | ||
var total = Object.keys(jobs).length, finished = 0; | ||
var keys = Object.keys(jobs), index = 0, quota = limit; | ||
var results = {}, errors = {}; | ||
var keys = Object.keys(jobs), total = keys.length; | ||
var completed = 0, started = 0, running = 0; | ||
var terminated = false; | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
yield (() => { | ||
while (quota) { | ||
var job = keys[index++]; | ||
quota--; | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
yield (() => { | ||
while (running < limit && started < total) { | ||
started++; | ||
running++; | ||
var job = keys[started-1]; | ||
if (index > total) continue; | ||
jobs[job](((job) => ((err, result, quit) => { | ||
if (terminated) return; | ||
if (kill && err) { | ||
terminated = true; | ||
return done(err); | ||
} | ||
jobs[job](((job) => ((err, result) => { | ||
if (total == -1) return; | ||
if (kill && err) { | ||
total = -1; | ||
return done(err); | ||
} | ||
if (err) errors[job] = err; | ||
results[job] = result||null; | ||
if (total == ++finished) | ||
return done((Object.keys(errors).length ? errors : null), results); | ||
if (err) errors[job] = err; | ||
results[job] = result||null; | ||
quota++; | ||
setTimeout(() => it.next(), 0); | ||
}))(job)); | ||
} | ||
})(); | ||
yield* iterator(); | ||
} | ||
if (quit) { | ||
terminated = true; | ||
return done(check(errors), results, quit); | ||
} | ||
completed++; | ||
running--; | ||
if (completed >= total) | ||
return done(check(errors), results); | ||
setTimeout(() => it.next(), 0); | ||
}))(job)); | ||
} | ||
})(); | ||
yield* iterator(); | ||
} | ||
} | ||
@@ -195,12 +229,12 @@ | ||
function forever (job, done) { | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
yield job((err) => { | ||
if (err) return done(err); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
yield* iterator(); | ||
} | ||
function* iterator () { | ||
yield job((err) => { | ||
if (err) return done(err); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
yield* iterator(); | ||
} | ||
} | ||
@@ -210,13 +244,13 @@ | ||
function until (test, job, done) { | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
yield job((err) => { | ||
if (err) return done(err); | ||
if (test()) return done(); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
yield* iterator(); | ||
} | ||
function* iterator () { | ||
yield job((err) => { | ||
if (err) return done(err); | ||
if (test()) return done(); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
yield* iterator(); | ||
} | ||
} | ||
@@ -226,13 +260,13 @@ | ||
function whilst (test, job, done) { | ||
var it = iterator(); | ||
it.next(); | ||
var it = iterator(); | ||
it.next(); | ||
function* iterator () { | ||
yield job((err) => { | ||
if (err) return done(err); | ||
if (!test()) return done(); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
yield* iterator(); | ||
} | ||
function* iterator () { | ||
yield job((err) => { | ||
if (err) return done(err); | ||
if (!test()) return done(); | ||
setTimeout(() => it.next(), 0); | ||
}); | ||
yield* iterator(); | ||
} | ||
} | ||
@@ -242,15 +276,17 @@ | ||
return { | ||
series: series, | ||
eachSeries: eachSeries, | ||
each: each, | ||
eachLimit: eachLimit, | ||
series: series, | ||
eachSeries: eachSeries, | ||
each: each, | ||
eachLimit: eachLimit, | ||
parallel: parallel, | ||
parallelLimit: parallelLimit, | ||
parallel: parallel, | ||
parallelLimit: parallelLimit, | ||
forever: forever, | ||
until: until, | ||
whilst: whilst | ||
forever: forever, | ||
until: until, | ||
whilst: whilst | ||
}; | ||
})); |
{ | ||
"name": "async-harmony", | ||
"version": "1.0.0", | ||
"description": "Asynchronous Harmony", | ||
"keywords": ["async", "harmony", "asynchronous", "sequential", "parallel"], | ||
"license": "MIT", | ||
"homepage": "http://github.com/simov/async-harmony/", | ||
"author": { | ||
"name": "simo", | ||
"email": "simeonvelichkov@gmail.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/simov/async-harmony.git" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
}, | ||
"main": "./lib/async-harmony" | ||
"name": "async-harmony", | ||
"version": "1.0.1", | ||
"description": "Asynchronous Harmony", | ||
"keywords": ["async", "harmony", "asynchronous", "sequential", "parallel"], | ||
"license": "MIT", | ||
"homepage": "http://github.com/simov/async-harmony/", | ||
"author": { | ||
"name": "simo", | ||
"email": "simeonvelichkov@gmail.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/simov/async-harmony.git" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
}, | ||
"main": "./lib/async-harmony" | ||
} |
148
README.md
@@ -36,5 +36,5 @@ | ||
- _**jobs**_ - `[]` or `{}` of jobs to execute | ||
- each job is `function(args, done)` where | ||
- `args` - contains the results from all previous jobs, and intermediate results passed between the jobs | ||
- `done(err, result)` - return error and result | ||
- each job is `function(args, done)` where | ||
- `args` - contains the results from all previous jobs, and intermediate results passed between the jobs | ||
- `done(err, result)` - return error and result | ||
- _**done(err, result)**_ - callback to execute once all jobs are done | ||
@@ -45,10 +45,10 @@ - _**kill**_ - `true` or `false` - determines whether to stop the job queue on first error received or not | ||
async.series({ | ||
job1: (args, done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
done(null, 'result2'); | ||
} | ||
job1: (args, done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
done(null, 'result2'); | ||
} | ||
}, (err, result) => { | ||
result == {job1:'result1', job2:'result2'} | ||
result == {job1:'result1', job2:'result2'} | ||
}); | ||
@@ -64,3 +64,3 @@ ``` | ||
- _**job**_ - `function(err, done)` to execute for each item | ||
- `done(err, result)` - return error and result | ||
- `done(err, result)` - return error and result | ||
- _**done(err, result)**_ - callback to execute once all items are processed | ||
@@ -71,5 +71,5 @@ - _**kill**_ - `true` or `false` - determines whether to stop the job queue on first error received or not | ||
async.eachSeries([0,1], (item, done) => { | ||
done(null, 'result'+item); | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
result == ['result0', 'result1'] | ||
result == ['result0', 'result1'] | ||
}); | ||
@@ -85,3 +85,3 @@ ``` | ||
- _**job**_ - `function(err, done)` to execute for each item | ||
- `done(err, result)` - return error and result | ||
- `done(err, result)` - return error and result | ||
- _**done(err, result)**_ - callback to execute once all items are processed | ||
@@ -92,5 +92,5 @@ - _**kill**_ - `true` or `false` - determines whether to stop the job queue on first error received or not | ||
async.each([0,1], (item, done) => { | ||
done(null, 'result'+item); | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
result == ['result0', 'result1'] | ||
result == ['result0', 'result1'] | ||
}); | ||
@@ -107,3 +107,3 @@ ``` | ||
- _**job**_ - `function(err, done)` to execute for each item | ||
- `done(err, result)` - return error and result | ||
- `done(err, result)` - return error and result | ||
- _**done(err, result)**_ - callback to execute once all items are processed | ||
@@ -114,5 +114,5 @@ - _**kill**_ - `true` or `false` - determines whether to stop the job queue on first error received or not | ||
async.eachLimit(2, [0,1,2], (item, done) => { | ||
done(null, 'result'+item); | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
result == ['result0', 'result1', 'result2'] | ||
result == ['result0', 'result1', 'result2'] | ||
}); | ||
@@ -127,4 +127,4 @@ ``` | ||
- _**jobs**_ - `[]` or `{}` of jobs to execute | ||
- each job is `function(done)` where | ||
- `done(err, result)` - return error and result | ||
- each job is `function(done)` where | ||
- `done(err, result)` - return error and result | ||
- _**done(err, result)**_ - callback to execute once all jobs are done | ||
@@ -135,10 +135,10 @@ - _**kill**_ - `true` or `false` - determines whether to stop the job queue on first error received or not | ||
async.parallel({ | ||
job1: (done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(null, 'result2'); | ||
} | ||
job1: (done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(null, 'result2'); | ||
} | ||
}, (err, result) => { | ||
result == {job1:'result1', job2:'result2'} | ||
result == {job1:'result1', job2:'result2'} | ||
}); | ||
@@ -154,4 +154,4 @@ ``` | ||
- _**jobs**_ - `[]` or `{}` of jobs to execute | ||
- each job is `function(done)` where | ||
- `done(err, result)` - return error and result | ||
- each job is `function(done)` where | ||
- `done(err, result)` - return error and result | ||
- _**done(err, result)**_ - callback to execute once all jobs are done | ||
@@ -162,13 +162,13 @@ - _**kill**_ - `true` or `false` - determines whether to stop the job queue on first error received or not | ||
async.parallelLimit(2, { | ||
job1: (done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(null, 'result2'); | ||
}, | ||
job3: (done) => { | ||
done(null, 'result3'); | ||
} | ||
job1: (done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(null, 'result2'); | ||
}, | ||
job3: (done) => { | ||
done(null, 'result3'); | ||
} | ||
}, (err, result) => { | ||
result == {job1:'result1', job2:'result2', job2:'result3'} | ||
result == {job1:'result1', job2:'result2', job2:'result3'} | ||
}); | ||
@@ -183,4 +183,4 @@ ``` | ||
- _**job**_ - task to execute | ||
- job is `function(done)` where | ||
- `done(err)` - return error | ||
- job is `function(done)` where | ||
- `done(err)` - return error | ||
- _**done(err)**_ - callback to execute if an error occur | ||
@@ -191,9 +191,9 @@ | ||
async.forever((done) => { | ||
setTimeout(() => { | ||
b = !b; | ||
console.log(b ? 'tik' : 'tak'); | ||
done(); | ||
}, 1000); | ||
setTimeout(() => { | ||
b = !b; | ||
console.log(b ? 'tik' : 'tak'); | ||
done(); | ||
}, 1000); | ||
}, (err) => { | ||
if (err) console.log('Clock stopped!'); | ||
if (err) console.log('Clock stopped!'); | ||
}); | ||
@@ -209,4 +209,4 @@ ``` | ||
- _**job**_ - task to execute | ||
- job is `function(done)` where | ||
- `done(err)` - return error | ||
- job is `function(done)` where | ||
- `done(err)` - return error | ||
- _**done(err)**_ - callback to execute when the test condition is met or if an error occur | ||
@@ -217,13 +217,13 @@ | ||
async.until( | ||
() => (count == 5), | ||
(done) => { | ||
setTimeout(() => { | ||
console.log(++count, 'seconds passed!'); | ||
done(); | ||
}, 1000); | ||
}, | ||
(err) => { | ||
if (err) console.log('Counter broke!'); | ||
else console.log('Done: 5 seconds passed!'); | ||
} | ||
() => (count == 5), | ||
(done) => { | ||
setTimeout(() => { | ||
console.log(++count, 'seconds passed!'); | ||
done(); | ||
}, 1000); | ||
}, | ||
(err) => { | ||
if (err) console.log('Counter broke!'); | ||
else console.log('Done: 5 seconds passed!'); | ||
} | ||
); | ||
@@ -239,4 +239,4 @@ ``` | ||
- _**job**_ - task to execute | ||
- job is `function(done)` where | ||
- `done(err)` - return error | ||
- job is `function(done)` where | ||
- `done(err)` - return error | ||
- _**done(err)**_ - callback to execute when the test condition becomes false or if an error occur | ||
@@ -247,13 +247,13 @@ | ||
async.whilst( | ||
() => (count < 5), | ||
(done) => { | ||
setTimeout(() => { | ||
console.log(++count, 'seconds passed!'); | ||
done(); | ||
}, 1000); | ||
}, | ||
(err) => { | ||
if (err) console.log('Counter broke!'); | ||
else console.log('Done: 5 seconds passed!'); | ||
} | ||
() => (count < 5), | ||
(done) => { | ||
setTimeout(() => { | ||
console.log(++count, 'seconds passed!'); | ||
done(); | ||
}, 1000); | ||
}, | ||
(err) => { | ||
if (err) console.log('Counter broke!'); | ||
else console.log('Done: 5 seconds passed!'); | ||
} | ||
); | ||
@@ -260,0 +260,0 @@ ``` |
239
test/each.js
describe('eachSeries', function () { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(new Error('error'+item)); | ||
}, (err, result) => { | ||
err.message.should.equal('error0'); | ||
done(); | ||
}); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(new Error('error'+item), 'result'+item); | ||
}, (err, result) => { | ||
err[0].message.should.equal('error0'); | ||
err[1].message.should.equal('error1'); | ||
result.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}, false); | ||
}); | ||
describe('eachSeries', () => { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(new Error('error'+item)); | ||
}, (err, results) => { | ||
err.message.should.equal('error0'); | ||
done(); | ||
}); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(new Error('error'+item), 'result'+item); | ||
}, (err, results) => { | ||
err[0].message.should.equal('error0'); | ||
err[1].message.should.equal('error1'); | ||
results.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}, false); | ||
}); | ||
it('terminate execution on quit flag passed', (done) => { | ||
async.eachSeries([0,1], (item, done) => { | ||
done(null, 'result'+item, true); | ||
}, (err, results, quit) => { | ||
should.equal(err, null); | ||
results.should.deep.equal(['result0']); | ||
quit.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('each', function () { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(new Error('error'+item)); | ||
}, (err, result) => { | ||
err.message.should.equal('error0'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(new Error('error'+item), 'result'+item); | ||
}, (err, result) => { | ||
err[0].message.should.equal('error0'); | ||
err[1].message.should.equal('error1'); | ||
result.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}); | ||
}); | ||
describe('each', () => { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(null, 'result'+item); | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(new Error('error'+item)); | ||
}, (err, results) => { | ||
err.message.should.equal('error0'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(new Error('error'+item), 'result'+item); | ||
}, (err, results) => { | ||
err[0].message.should.equal('error0'); | ||
err[1].message.should.equal('error1'); | ||
results.should.deep.equal(['result0', 'result1']); | ||
done(); | ||
}); | ||
}); | ||
it('terminate execution on quit flag passed', (done) => { | ||
async.each([0,1], (item, done) => { | ||
done(null, 'result'+item, true); | ||
}, (err, results, quit) => { | ||
should.equal(err, null); | ||
results.should.deep.equal(['result0']); | ||
quit.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('eachLimit', function () { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.eachLimit(2, [0,1,2], (item, done) => { | ||
done(null, 'result'+item); | ||
}, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal(['result0', 'result1', 'result2']); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.eachLimit(2, [0,1], (item, done) => { | ||
done(new Error('error'+item)); | ||
}, (err, result) => { | ||
err.message.should.equal('error0'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.eachLimit(2, [0,1,2], (item, done) => { | ||
done(new Error('error'+item), 'result'+item); | ||
}, (err, result) => { | ||
err[0].message.should.equal('error0'); | ||
err[1].message.should.equal('error1'); | ||
err[2].message.should.equal('error2'); | ||
result.should.deep.equal(['result0', 'result1', 'result2']); | ||
done(); | ||
}); | ||
}); | ||
describe('eachLimit', () => { | ||
'use strict'; | ||
it('limit', (done) => { | ||
var items = [1,2,3,4,5,6,7,8], | ||
time = [25,100,50,50,25,50,100,25], | ||
index = 0; | ||
async.eachLimit(3, items, (item, done) => { | ||
setTimeout(() => { | ||
done(null, 'result'+item); | ||
}, time[index++]); | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
if (results[2] == 'result5') | ||
results.should.deep.equal([ | ||
'result1', 'result3', 'result5', 'result4', | ||
'result2', 'result8', 'result6', 'result7' | ||
]); | ||
else | ||
// finish at the same time, actual order is not guaranteed | ||
results.should.deep.equal([ | ||
'result1', 'result3', 'result4', 'result5', | ||
'result2', 'result8', 'result6', 'result7' | ||
]); | ||
done(); | ||
}); | ||
}); | ||
it('return result when done', (done) => { | ||
async.eachLimit(2, [0,1,2], (item, done) => { | ||
done(null, 'result'+item); | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal(['result0', 'result1', 'result2']); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.eachLimit(2, [0,1], (item, done) => { | ||
done(new Error('error'+item)); | ||
}, (err, results) => { | ||
err.message.should.equal('error0'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.eachLimit(2, [0,1,2], (item, done) => { | ||
done(new Error('error'+item), 'result'+item); | ||
}, (err, results) => { | ||
err[0].message.should.equal('error0'); | ||
err[1].message.should.equal('error1'); | ||
err[2].message.should.equal('error2'); | ||
results.should.deep.equal(['result0', 'result1', 'result2']); | ||
done(); | ||
}); | ||
}); | ||
it('terminate execution on quit flag passed', (done) => { | ||
async.eachLimit(2, [0,1,2], (item, done) => { | ||
done(null, 'result'+item, true); | ||
}, (err, results, quit) => { | ||
should.equal(err, null); | ||
results.should.deep.equal(['result0']); | ||
quit.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); |
describe('parallel', function () { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
var obj = { | ||
job1: (done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(null, 'result2'); | ||
} | ||
}; | ||
var arr = [ | ||
(done) => { | ||
done(null, 'result1'); | ||
}, | ||
(done) => { | ||
done(null, 'result2'); | ||
} | ||
]; | ||
async.parallel(obj, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal({job1:'result1', job2:'result2'}); | ||
describe('parallel', () => { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.parallel({ | ||
job1: (done) => done(null, 'result1'), | ||
job2: (done) => done(null, 'result2') | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal({job1:'result1', job2:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.parallel({ | ||
job1: (done) => done(new Error('error1')), | ||
job2: (done) => done(new Error('error2')) | ||
}, (err, results) => { | ||
err.message.should.equal('error1'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.parallel({ | ||
job1: (done) => done(new Error('error1'), 'result1'), | ||
job2: (done) => done(new Error('error2'), 'result2') | ||
}, (err, results) => { | ||
err.job1.message.should.equal('error1'); | ||
err.job2.message.should.equal('error2'); | ||
results.should.deep.equal({job1:'result1', job2:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
it('pass on errors and store them - array', (done) => { | ||
async.parallel([ | ||
(done) => done(new Error('error1'), 'result1'), | ||
(done) => done(new Error('error2'), 'result2') | ||
], (err, results) => { | ||
err[0].message.should.equal('error1'); | ||
err[1].message.should.equal('error2'); | ||
results.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
it('accept jobs array', (done) => { | ||
async.parallel([ | ||
(done) => done(null, 'result1'), | ||
(done) => done(null, 'result2') | ||
], (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
it('terminate execution on quit flag passed', (done) => { | ||
async.parallel({ | ||
job1: (done) => done(null, 'result1', true), | ||
job2: (done) => done(null, 'result2') | ||
}, (err, results, quit) => { | ||
should.equal(err, null); | ||
results.should.deep.equal({job1:'result1'}); | ||
quit.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
async.parallel(arr, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.parallel({ | ||
job1: (done) => { | ||
done(new Error('error1')); | ||
}, | ||
job2: (done) => { | ||
done(new Error('error2')); | ||
} | ||
}, (err, result) => { | ||
err.message.should.equal('error1'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
var obj = { | ||
job1: (done) => { | ||
done(new Error('error1'), 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(new Error('error2'), 'result2'); | ||
} | ||
}; | ||
var arr = [ | ||
(done) => { | ||
done(new Error('error1'), 'result1'); | ||
}, | ||
(done) => { | ||
done(new Error('error2'), 'result2'); | ||
} | ||
]; | ||
async.parallel(obj, (err, result) => { | ||
err.job1.message.should.equal('error1'); | ||
err.job2.message.should.equal('error2'); | ||
result.should.deep.equal({job1:'result1', job2:'result2'}); | ||
describe('parallelLimit', () => { | ||
'use strict'; | ||
it('limit', (done) => { | ||
var items = [1,2,3,4,5,6,7,8], | ||
time = [25,100,50,50,25,50,100,25], | ||
index = 0; | ||
async.parallel(arr, (err, result) => { | ||
err[0].message.should.equal('error1'); | ||
err[1].message.should.equal('error2'); | ||
result.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
async.parallelLimit(3, { | ||
job1: (done) => {setTimeout(() => done(null, 'result1'), time[index++])}, | ||
job2: (done) => {setTimeout(() => done(null, 'result2'), time[index++])}, | ||
job3: (done) => {setTimeout(() => done(null, 'result3'), time[index++])}, | ||
job4: (done) => {setTimeout(() => done(null, 'result4'), time[index++])}, | ||
job5: (done) => {setTimeout(() => done(null, 'result5'), time[index++])}, | ||
job6: (done) => {setTimeout(() => done(null, 'result6'), time[index++])}, | ||
job7: (done) => {setTimeout(() => done(null, 'result7'), time[index++])}, | ||
job8: (done) => {setTimeout(() => done(null, 'result8'), time[index++])} | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
if (Object.keys(results)[2] == 'job5') | ||
results.should.deep.equal({ | ||
job1: 'result1', job3: 'result3', job5: 'result5', job4: 'result4', | ||
job2: 'result2', job8: 'result8', job6: 'result6', job7: 'result7' | ||
}); | ||
else | ||
// finish at the same time, actual order is not guaranteed | ||
results.should.deep.equal({ | ||
job1: 'result1', job3: 'result3', job4: 'result4', job5: 'result5', | ||
job2: 'result2', job8: 'result8', job6: 'result6', job7: 'result7' | ||
}); | ||
done(); | ||
}); | ||
}); | ||
it('return result when done', (done) => { | ||
async.parallelLimit(2, { | ||
job1: (done) => done(null, 'result1'), | ||
job2: (done) => done(null, 'result2'), | ||
job3: (done) => done(null, 'result3') | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal({job1:'result1', job2:'result2', job3:'result3'}); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.parallelLimit(2, { | ||
job1: (done) => done(new Error('error1')), | ||
job2: (done) => done(new Error('error2')), | ||
job3: (done) => done(new Error('error3')) | ||
}, (err, results) => { | ||
err.message.should.equal('error1'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.parallelLimit(2, { | ||
job1: (done) => done(new Error('error1'), 'result1'), | ||
job2: (done) => done(new Error('error2'), 'result2'), | ||
job3: (done) => done(new Error('error3'), 'result3') | ||
}, (err, results) => { | ||
err.job1.message.should.equal('error1'); | ||
err.job2.message.should.equal('error2'); | ||
err.job3.message.should.equal('error3'); | ||
results.should.deep.equal({job1:'result1', job2:'result2', job3:'result3'}); | ||
done(); | ||
}); | ||
}); | ||
it('terminate execution on quit flag passed', (done) => { | ||
async.parallelLimit(2,{ | ||
job1: (done) => done(null, 'result1', true), | ||
job2: (done) => done(null, 'result2'), | ||
job3: (done) => done(null, 'result3') | ||
}, (err, results, quit) => { | ||
should.equal(err, null); | ||
results.should.deep.equal({job1:'result1'}); | ||
quit.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('parallelLimit', function () { | ||
'use strict'; | ||
it('return result when done', (done) => { | ||
async.parallelLimit(2, { | ||
job1: (done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(null, 'result2'); | ||
}, | ||
job3: (done) => { | ||
done(null, 'result3'); | ||
} | ||
}, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal({job1:'result1', job2:'result2', job3:'result3'}); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.parallelLimit(2, { | ||
job1: (done) => { | ||
done(new Error('error1')); | ||
}, | ||
job2: (done) => { | ||
done(new Error('error2')); | ||
} | ||
}, (err, result) => { | ||
err.message.should.equal('error1'); | ||
done(); | ||
}, true); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.parallelLimit(2, { | ||
job1: (done) => { | ||
done(new Error('error1'), 'result1'); | ||
}, | ||
job2: (done) => { | ||
done(new Error('error2'), 'result2'); | ||
}, | ||
job3: (done) => { | ||
done(new Error('error3'), 'result3'); | ||
} | ||
}, (err, result) => { | ||
err.job1.message.should.equal('error1'); | ||
err.job2.message.should.equal('error2'); | ||
err.job3.message.should.equal('error3'); | ||
result.should.deep.equal({job1:'result1', job2:'result2', job3:'result3'}); | ||
done(); | ||
}); | ||
}); | ||
}); |
describe('series', () => { | ||
'use strict'; | ||
describe('series', function () { | ||
'use strict'; | ||
// args - callback args | ||
// result - final callback result | ||
// job - callback job | ||
// job result - done(null, 'job result') | ||
it('pass job result to args', (done) => { | ||
async.series({ | ||
job1: (args, done) => done(null, 'result1'), | ||
job2: (args, done) => { | ||
args.job1.should.equal('result1'); | ||
done(); | ||
} | ||
}, (err, results) => done(err)); | ||
}); | ||
it('pass job result to args', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.job1.should.equal('result1'); | ||
done(); | ||
} | ||
}, (err, result) => { | ||
done(err); | ||
}); | ||
}); | ||
it('pass custom data to args', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
args.data = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.data.should.equal('job1'); | ||
done(); | ||
} | ||
}, (err, result) => { | ||
done(err); | ||
}); | ||
}); | ||
it('job keys are overriden in args', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
args.job1 = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.job1.should.equal('result1'); | ||
done(); | ||
} | ||
}, (err, result) => { | ||
done(err); | ||
}); | ||
}); | ||
it('job result in args is a shallow copy', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
done(null, {data:'result1'}); | ||
}, | ||
job2: (args, done) => { | ||
args.job1.data = 'result2'; | ||
done(); | ||
} | ||
}, (err, result) => { | ||
if (err) return done(err); | ||
result.job1.data.should.equal('result2'); | ||
done(); | ||
}); | ||
}); | ||
it('pass custom data to args', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
args.data = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.data.should.equal('job1'); | ||
done(); | ||
} | ||
}, (err, results) => done(err)); | ||
}); | ||
it('return only result when done', (done) => { | ||
var obj = { | ||
job1: (args, done) => { | ||
args.data1 = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.data2 = 'job2'; | ||
done(null, 'result2'); | ||
} | ||
}; | ||
var arr = [ | ||
(args, done) => { | ||
args.data1 = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
(args, done) => { | ||
args.data2 = 'job2'; | ||
done(null, 'result2'); | ||
} | ||
]; | ||
async.series(obj, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal({job1:'result1', job2:'result2'}); | ||
async.series(arr, (err, result) => { | ||
if (err) return done(err); | ||
result.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
done(new Error('error1')); | ||
}, | ||
job2: (args, done) => { | ||
done(new Error('error2')); | ||
} | ||
}, (err, result) => { | ||
err.message.should.equal('error1'); | ||
done(); | ||
}); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
var obj = { | ||
job1: (args, done) => { | ||
done(new Error('error1'), 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
done(new Error('error2'), 'result2'); | ||
} | ||
}; | ||
var arr = [ | ||
(args, done) => { | ||
done(new Error('error1'), 'result1'); | ||
}, | ||
(args, done) => { | ||
done(new Error('error2'), 'result2'); | ||
} | ||
]; | ||
async.series(obj, (err, result) => { | ||
err.job1.message.should.equal('error1'); | ||
err.job2.message.should.equal('error2'); | ||
result.should.deep.equal({job1:'result1', job2:'result2'}); | ||
async.series(arr, (err, result) => { | ||
err[0].message.should.equal('error1'); | ||
err[1].message.should.equal('error2'); | ||
result.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}, false); | ||
}, false); | ||
}); | ||
it('exclude custom data from results', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
args.data1 = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.data2 = 'job2'; | ||
done(null, 'result2'); | ||
} | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal({job1:'result1', job2:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
it('override job keys in args', (done) => { | ||
async.series({ | ||
job1: (args, done) => { | ||
args.job1 = 'job1'; | ||
done(null, 'result1'); | ||
}, | ||
job2: (args, done) => { | ||
args.job1.should.equal('result1'); | ||
done(); | ||
} | ||
}, (err, results) => done(err)); | ||
}); | ||
it('job result in args is shallow copy', (done) => { | ||
async.series({ | ||
job1: (args, done) => done(null, {data:'result1'}), | ||
job2: (args, done) => { | ||
args.job1.data = 'result2'; | ||
done(); | ||
} | ||
}, (err, results) => { | ||
if (err) return done(err); | ||
results.job1.data.should.equal('result2'); | ||
done(); | ||
}); | ||
}); | ||
it('stop on first error received', (done) => { | ||
async.series({ | ||
job1: (args, done) => done(new Error('error1')), | ||
job2: (args, done) => done(new Error('error2')) | ||
}, (err, results) => { | ||
err.message.should.equal('error1'); | ||
done(); | ||
}); | ||
}); | ||
it('pass on errors and store them', (done) => { | ||
async.series({ | ||
job1: (args, done) => done(new Error('error1'), 'result1'), | ||
job2: (args, done) => done(new Error('error2'), 'result2') | ||
}, (err, results) => { | ||
err.job1.message.should.equal('error1'); | ||
err.job2.message.should.equal('error2'); | ||
results.should.deep.equal({job1:'result1', job2:'result2'}); | ||
done(); | ||
}, false); | ||
}); | ||
it('accept jobs array', (done) => { | ||
async.series([ | ||
(args, done) => done(null, 'result1'), | ||
(args, done) => done(null, 'result2') | ||
], (err, results) => { | ||
if (err) return done(err); | ||
results.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}); | ||
}); | ||
it('pass on errors and store them - array', (done) => { | ||
async.series([ | ||
(args, done) => done(new Error('error1'), 'result1'), | ||
(args, done) => done(new Error('error2'), 'result2') | ||
], (err, results) => { | ||
err[0].message.should.equal('error1'); | ||
err[1].message.should.equal('error2'); | ||
results.should.deep.equal({0:'result1', 1:'result2'}); | ||
done(); | ||
}, false); | ||
}); | ||
it('terminate execution on quit flag passed', (done) => { | ||
async.series({ | ||
job1: (args, done) => done(null, 'result1', true), | ||
job2: (args, done) => done(null, 'result2') | ||
}, (err, results, quit) => { | ||
should.equal(err, null); | ||
results.should.deep.equal({job1:'result1'}); | ||
quit.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); |
describe('forever', function () { | ||
it('process until error occures', (done) => { | ||
var result = [], count = 0; | ||
async.forever((done) => { | ||
if (count == 2) return done(new Error('error')); | ||
result.push(++count); | ||
done(); | ||
}, (err) => { | ||
err.message.should.equal('error'); | ||
result.length.should.equal(2); | ||
done(); | ||
}); | ||
}); | ||
describe('forever', () => { | ||
it('process until error occures', (done) => { | ||
var result = [], count = 0; | ||
async.forever((done) => { | ||
if (count == 2) return done(new Error('error')); | ||
result.push(++count); | ||
done(); | ||
}, (err) => { | ||
err.message.should.equal('error'); | ||
result.length.should.equal(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('until', function () { | ||
it('process until test condition is met', (done) => { | ||
var result = [], count = 0; | ||
async.until( | ||
() => (count == 5), | ||
(done) => { | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
if (err) return done(err); | ||
result.length.should.equal(5); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('process until error occures', (done) => { | ||
var result = [], count = 0; | ||
async.until( | ||
() => (count == 5), | ||
(done) => { | ||
if (count == 2) return done(new Error('error')); | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
err.message.should.equal('error'); | ||
result.length.should.equal(2); | ||
done(); | ||
} | ||
); | ||
}); | ||
describe('until', () => { | ||
it('process until test condition is met', (done) => { | ||
var result = [], count = 0; | ||
async.until( | ||
() => (count == 5), | ||
(done) => { | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
if (err) return done(err); | ||
result.length.should.equal(5); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('process until error occures', (done) => { | ||
var result = [], count = 0; | ||
async.until( | ||
() => (count == 5), | ||
(done) => { | ||
if (count == 2) return done(new Error('error')); | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
err.message.should.equal('error'); | ||
result.length.should.equal(2); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
describe('whilst', function () { | ||
it('process while test condition is true', (done) => { | ||
var result = [], count = 0; | ||
async.whilst( | ||
() => (count < 5), | ||
(done) => { | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
if (err) return done(err); | ||
result.length.should.equal(5); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('process until error occures', (done) => { | ||
var result = [], count = 0; | ||
async.whilst( | ||
() => (count < 5), | ||
(done) => { | ||
if (count == 2) return done(new Error('error')); | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
err.message.should.equal('error'); | ||
result.length.should.equal(2); | ||
done(); | ||
} | ||
); | ||
}); | ||
describe('whilst', () => { | ||
it('process while test condition is true', (done) => { | ||
var result = [], count = 0; | ||
async.whilst( | ||
() => (count < 5), | ||
(done) => { | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
if (err) return done(err); | ||
result.length.should.equal(5); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('process until error occures', (done) => { | ||
var result = [], count = 0; | ||
async.whilst( | ||
() => (count < 5), | ||
(done) => { | ||
if (count == 2) return done(new Error('error')); | ||
result.push(++count); | ||
done(); | ||
}, | ||
(err) => { | ||
err.message.should.equal('error'); | ||
result.length.should.equal(2); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40920
14
818