-
Notifications
You must be signed in to change notification settings - Fork 4
/
limit.js
35 lines (32 loc) · 938 Bytes
/
limit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
const arrayToObj = require('./lib/arrayToObj');
const objToArray = require('./lib/objToArray');
const some = require('./some');
async function limit (iterable, limit = 2, exitOnError = false) {
const [keys, items] = objToArray(iterable);
const allErrors = [];
const allValues = [];
const queue = [];
for (let value of items) {
queue.push(typeof value === 'function' ? value() : value);
if (queue.length === limit) {
const [errors, values] = await some(queue);
allErrors.push(...errors);
allValues.push(...values);
queue.length = 0;
if (exitOnError && errors.filter((err) => err).length > 0) {
break;
}
}
}
if (queue.length > 0) {
const [errors, values] = await some(queue);
allErrors.push(...errors);
allValues.push(...values);
}
return [
arrayToObj(keys, allErrors),
arrayToObj(keys, allValues)
];
}
module.exports = limit;