Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement <name> and <name>Series with <name>Limit #847

Merged
merged 1 commit into from
Mar 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/each.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import eachOf from './eachOf';
import withoutIndex from './internal/withoutIndex';
import eachLimit from './eachLimit';
import doLimit from './internal/doLimit';

export default function each(arr, iterator, cb) {
return eachOf(arr, withoutIndex(iterator), cb);
}
export default doLimit(eachLimit, Infinity);
34 changes: 3 additions & 31 deletions lib/eachOf.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
'use strict';

import once from 'lodash/once';
import noop from 'lodash/noop';
import eachOfLimit from './eachOfLimit';
import doLimit from './internal/doLimit';

import keyIterator from './internal/keyIterator';
import onlyOnce from './internal/onlyOnce';

export default function eachOf(object, iterator, callback) {
callback = once(callback || noop);
object = object || [];

var iter = keyIterator(object);
var key, completed = 0;

while ((key = iter()) != null) {
completed += 1;
iterator(object[key], key, onlyOnce(done));
}

if (completed === 0) callback(null);

function done(err) {
completed--;
if (err) {
callback(err);
}
// Check key is null in case iterator isn't exhausted
// and done resolved synchronously.
else if (key === null && completed <= 0) {
callback(null);
}
}
}
export default doLimit(eachOfLimit, Infinity);
40 changes: 3 additions & 37 deletions lib/eachOfSeries.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
'use strict';

import once from 'lodash/once';
import noop from 'lodash/noop';
import eachOfLimit from './eachOfLimit';
import doLimit from './internal/doLimit';

import keyIterator from './internal/keyIterator';
import onlyOnce from './internal/onlyOnce';
import setImmediate from './setImmediate';

export default function eachOfSeries(obj, iterator, callback) {
callback = once(callback || noop);
obj = obj || [];
var nextKey = keyIterator(obj);
var key = nextKey();

function iterate() {
var sync = true;
if (key === null) {
return callback(null);
}
iterator(obj[key], key, onlyOnce(function(err) {
if (err) {
callback(err);
} else {
key = nextKey();
if (key === null) {
return callback(null);
} else {
if (sync) {
setImmediate(iterate);
} else {
iterate();
}
}
}
}));
sync = false;
}
iterate();
}
export default doLimit(eachOfLimit, 1);
8 changes: 3 additions & 5 deletions lib/eachSeries.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import eachOfSeries from './eachOfSeries';
import withoutIndex from './internal/withoutIndex';
import eachLimit from './eachLimit';
import doLimit from './internal/doLimit';

export default function eachSeries(arr, iterator, cb) {
return eachOfSeries(arr, withoutIndex(iterator), cb);
}
export default doLimit(eachLimit, 1);
7 changes: 3 additions & 4 deletions lib/every.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

import createTester from './internal/createTester';
import eachOf from './eachOf';
import notId from './internal/notId';
import everyLimit from './everyLimit';
import doLimit from './internal/doLimit';

export default createTester(eachOf, notId, notId);
export default doLimit(everyLimit, Infinity);
6 changes: 3 additions & 3 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import filter from './internal/filter';
import doParallel from './internal/doParallel';
import filterLimit from './filterLimit';
import doLimit from './internal/doLimit';

export default doParallel(filter);
export default doLimit(filterLimit, Infinity);
6 changes: 3 additions & 3 deletions lib/filterSeries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import filter from './internal/filter';
import doSeries from './internal/doSeries';
import filterLimit from './filterLimit';
import doLimit from './internal/doLimit';

export default doSeries(filter);
export default doLimit(filterLimit, 1);
7 changes: 7 additions & 0 deletions lib/internal/doLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

export default function doLimit(fn, limit) {
return function (iterable, iterator, callback) {
return fn(iterable, limit, iterator, callback);
};
}
6 changes: 3 additions & 3 deletions lib/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import doParallel from './internal/doParallel';
import map from './internal/map';
import mapLimit from './mapLimit';
import doLimit from './internal/doLimit';

export default doParallel(map);
export default doLimit(mapLimit, Infinity);
6 changes: 3 additions & 3 deletions lib/mapSeries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import map from './internal/map';
import doSeries from './internal/doSeries';
import mapLimit from './mapLimit';
import doLimit from './internal/doLimit';

export default doSeries(map);
export default doLimit(mapLimit, 1);
8 changes: 3 additions & 5 deletions lib/parallel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import _parallel from './internal/parallel';
import eachOf from './eachOf';
import parallelLimit from './parallelLimit';
import doLimit from './internal/doLimit';

export default function parallel(tasks, cb) {
return _parallel(eachOf, tasks, cb);
}
export default doLimit(parallelLimit, Infinity);
6 changes: 3 additions & 3 deletions lib/reject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import reject from './internal/reject';
import doParallel from './internal/doParallel';
import rejectLimit from './rejectLimit';
import doLimit from './internal/doLimit';

export default doParallel(reject);
export default doLimit(rejectLimit, Infinity);
6 changes: 3 additions & 3 deletions lib/rejectSeries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import reject from './internal/reject';
import doSeries from './internal/doSeries';
import rejectLimit from './rejectLimit';
import doLimit from './internal/doLimit';

export default doSeries(reject);
export default doLimit(rejectLimit, 1);
8 changes: 3 additions & 5 deletions lib/some.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import identity from 'lodash/identity';
import someLimit from './someLimit';
import doLimit from './internal/doLimit';

import createTester from './internal/createTester';
import eachOf from './eachOf';

export default createTester(eachOf, Boolean, identity);
export default doLimit(someLimit, Infinity);
8 changes: 3 additions & 5 deletions lib/times.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import map from './map';
import range from 'lodash/_baseRange';
import timesLimit from './timesLimit';
import doLimit from './internal/doLimit';

export default function (count, iterator, callback) {
map(range(0, count, 1), iterator, callback);
}
export default doLimit(timesLimit, Infinity);
8 changes: 3 additions & 5 deletions lib/timesSeries.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import mapSeries from './mapSeries';
import range from 'lodash/_baseRange';
import timesLimit from './timesLimit';
import doLimit from './internal/doLimit';

export default function (count, iterator, callback) {
mapSeries(range(0, count, 1), iterator, callback);
}
export default doLimit(timesLimit, 1);
6 changes: 4 additions & 2 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,10 @@ exports['each extra callback'] = function(test){
var count = 0;
async.each([1,3,2], function(val, callback) {
count++;
var done = count == 3;
callback();
test.throws(callback);
if (count == 3) {
if (done) {
test.done();
}
});
Expand Down Expand Up @@ -1521,8 +1522,9 @@ exports['map'] = {
var r = [];
async.map(a, function(x, callback){
r.push(x);
var done = r.length == a.length;
callback(null);
if (r.length >= a.length) {
if (done) {
test.same(r, a);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the point of these changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fail without them. With how eachOfLimit is executed since the first iteration is synchronous, it executed the second iteration and so on so this causes multiple calls to test.done()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, alright that's breaking and should be fixed in v2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a 2.x branch we could merge this PR into. Although, I'm a bit hesitant because it could set us up for merge hell if we end up making lots of changes on master.

test.done();
}
Expand Down