Skip to content

Instantly share code, notes, and snippets.

@kvasdopil
Last active June 14, 2018 10:29
Show Gist options
  • Select an option

  • Save kvasdopil/468251c3aa9e0653e4292a0b55a610c8 to your computer and use it in GitHub Desktop.

Select an option

Save kvasdopil/468251c3aa9e0653e4292a0b55a610c8 to your computer and use it in GitHub Desktop.
function concat(args, func, cb) {
const results = [];
const errors = [];
args.forEach(arg => func(arg, (err, files) => {
if (err) {
errors.push(err);
} else {
results.push(files);
}
if (results.length + errors.length === args.length) {
cb(errors.length ? errors : null, results.reduce((a, i) => a.concat(i), []));
}
}));
}
function expect(a, b) {
console.log(a, b);
}
const callOrder = [];
const iteratee = (x, cb) => {
setTimeout(() => {
callOrder.push(x);
let y = x;
const r = [];
while (y > 0) {
r.push(y);
y -= 1;
}
cb(null, r);
}, x * 25);
};
concat([1, 3, 2], iteratee, (err, results) => {
expect(results, [1, 2, 1, 3, 2, 1]);
expect(callOrder, [1, 2, 3]);
expect(err, null);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment