Skip to content

Commit

Permalink
BREAKING CHANGE: remove partial application feature of applyEach (#1640)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: remove partial application feature of applyEach

* add awaitable tests for applyEach
  • Loading branch information
aearly committed May 20, 2019
1 parent 4330d53 commit 667e717
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/eachSeries.js
Expand Up @@ -6,7 +6,7 @@ import awaitify from './internal/awaitify'
*
* Note, that unlike [`each`]{@link module:Collections.each}, this function applies iteratee to each item
* in series and therefore the iteratee functions will complete in order.
* @name eachSeries
* @static
* @memberOf module:Collections
Expand Down
13 changes: 5 additions & 8 deletions lib/internal/applyEach.js
@@ -1,17 +1,14 @@
import initialParams from './initialParams';
import wrapAsync from './wrapAsync';
import awaitify from './awaitify'

export default function applyEach(eachfn) {
return function(fns, ...callArgs) {
var go = initialParams(function(args, callback) {
export default function (eachfn) {
return function applyEach(fns, ...callArgs) {
const go = awaitify(function (callback) {
var that = this;
return eachfn(fns, (fn, cb) => {
wrapAsync(fn).apply(that, args.concat(cb));
wrapAsync(fn).apply(that, callArgs.concat(cb));
}, callback);
});
if (callArgs.length) {
return go.apply(this, callArgs);
}
return go;
};
}
39 changes: 4 additions & 35 deletions test/applyEach.js
Expand Up @@ -27,7 +27,7 @@ describe('applyEach', () => {
cb(null, 3);
}, 18);
};
async.applyEach([one, two, three], 5, (err, results) => {
async.applyEach([one, two, three], 5)((err, results) => {
assert(err === null, err + " passed instead of 'null'");
expect(call_order).to.eql(['two', 'one', 'three']);
expect(results).to.eql([1, 2, 3]);
Expand All @@ -51,7 +51,7 @@ describe('applyEach', () => {
throw new Error('third task - should not get here');
}

async.applyEach({one, two, three}, 5, () => {
async.applyEach({one, two, three}, 5)(() => {
throw new Error('final callback - should not get here');
});

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('applyEach', () => {
cb(null, 3);
}, 15);
}
async.applyEachSeries([one, two, three], 5, (err, results) => {
async.applyEachSeries([one, two, three], 5)((err, results) => {
assert(err === null, err + " passed instead of 'null'");
expect(call_order).to.eql(['one', 'two', 'three']);
expect(results).to.eql([1, 2, 3]);
Expand All @@ -109,7 +109,7 @@ describe('applyEach', () => {
function three(/*, cb */) {
throw new Error('third task - should not get here');
}
async.applyEachSeries([one, two, three], 5, () => {
async.applyEachSeries([one, two, three], 5)(() => {
throw new Error('final callback - should not get here');
});

Expand All @@ -118,35 +118,4 @@ describe('applyEach', () => {
done();
}, 25);
});

it('applyEach partial application', (done) => {
var call_order = [];
var one = function (val, cb) {
expect(val).to.equal(5);
setTimeout(() => {
call_order.push('one');
cb(null, 1);
}, 10);
};
var two = function (val, cb) {
expect(val).to.equal(5);
setTimeout(() => {
call_order.push('two');
cb(null, 2);
}, 5);
};
var three = function (val, cb) {
expect(val).to.equal(5);
setTimeout(() => {
call_order.push('three');
cb(null, 3);
}, 15);
};
async.applyEach([one, two, three])(5, (err, results) => {
if (err) throw err;
expect(call_order).to.eql(['two', 'one', 'three']);
expect(results).to.eql([1, 2, 3]);
done();
});
});
});
4 changes: 2 additions & 2 deletions test/es2017/asyncFunctions.js
Expand Up @@ -272,14 +272,14 @@ module.exports = function () {
*/

it('should handle async functions in applyEach', (done) => {
async.applyEach([asyncIdentity, asyncIdentity])(input, (err, result) => {
async.applyEach([asyncIdentity, asyncIdentity], input)((err, result) => {
expect(result).to.eql([input, input]);
done(err);
});
});

it('should handle async functions in applyEachSeries', (done) => {
async.applyEachSeries([asyncIdentity, asyncIdentity])(input, (err, result) => {
async.applyEachSeries([asyncIdentity, asyncIdentity], input)((err, result) => {
expect(result).to.eql([input, input]);
done(err);
});
Expand Down
17 changes: 16 additions & 1 deletion test/es2017/awaitableFunctions.js
Expand Up @@ -316,7 +316,22 @@ module.exports = function () {
* Control flow
*/

// TODO: figure out to do with applyEach
it('should return a Promise: applyEach', async () => {
const calls = []
await async.applyEach([
async (v, x) => { calls.push(v, x) },
async (v, x) => { calls.push(v, x) }
], 5, 6)();
expect(calls).to.eql([5, 6, 5, 6])
})
it('should return a Promise: applyEachSeries', async () => {
const calls = []
await async.applyEachSeries([
async (v, x) => { calls.push(v, x) },
async (v, x) => { calls.push(v, x) }
], 5, 6)();
expect(calls).to.eql([5, 6, 5, 6])
})

it('should return a Promise: auto', async () => {
expect (async.auto.name).to.contain('auto')
Expand Down
12 changes: 6 additions & 6 deletions test/priorityQueue.js
Expand Up @@ -239,29 +239,29 @@ describe('priorityQueue', () => {
q.push('foo4', 1, () => {calls.push('foo4 cb');});
});
});

it('should not call the drain callback if receives empty push and tasks are still pending', (done) => {
var call_order = [];

var q = async.priorityQueue((task, callback) => {
call_order.push('process ' + task);
callback('error', 'arg');
}, 1);

q.push(1, 1, (err, arg) => {
expect(err).to.equal('error');
expect(arg).to.equal('arg');
call_order.push('callback ' + 1);
});

q.push(2, 1, (err, arg) => {
expect(err).to.equal('error');
expect(arg).to.equal('arg');
call_order.push('callback ' + 2);
});

expect(q.length()).to.equal(2);

q.drain = function () {
expect(call_order).to.eql([
'process 1', 'callback 1',
Expand All @@ -272,7 +272,7 @@ describe('priorityQueue', () => {
expect(q.running()).to.equal(0);
done();
};

q.push([], 1, () => {});
});
});
Expand Down

0 comments on commit 667e717

Please sign in to comment.