-
Notifications
You must be signed in to change notification settings - Fork 67
/
util.js
65 lines (61 loc) · 2.23 KB
/
util.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
var Q = require('q'),
logger = require('./setup').logger;
/**
* Run a function for each element in the list, with an interval between each
* run.
* Useful for spreading out processing batches so they don't happen too fast,
* which is especially helpful for Twitter API calls because it allows more
* connection reuse.
* @param {Array} list List of items to process
* @param {Number} interval Time in milliseconds to wait between each processing
* batch.
* @param {Function} f Function to call on each item. Should return a Promise.
* @return {Promise.<Array.<Object> >} A promise that resolves once all the
* component promises resolve, or resolves early if there is any failure.
*/
function slowForEach(list, interval, f) {
var promises = list.map(function(item, i) {
return Q.delay(i * interval).then(f.bind(null, item));
});
return Q.all(promises);
}
/**
* Apply f to each item in list, returning a Promise containing the results.
* f must return a Promise, and each application of f will wait on the
* resolution of the previously-returned Promise.
*
* This is different than slowForEach in that it doesn't need to create
* all the promises in advance, so it can be more memory-efficient when
* handling large arrays. It also doesn't have a predefined interval,
* but simply executes tasks sequentially.
*
* @param {Array} list List of items to process
* @param {Function} f Function to call on each item. Should return a Promise.
* @param {Array} accum Used internally. Callers should leave undefined.
* @return {Promise.<Array.<Object> >} A promise that resolves once all the
* component promises resolve, or resolves early if there is any failure.
*/
function promiseMap(list, f, accum) {
accum = accum || []
if (list.length === 0) {
return Q.resolve(accum);
}
return f(list[0]
).then(function(res) {
return promiseMap(list.slice(1), f, accum.concat([res]));
});
}
module.exports = {
slowForEach: slowForEach,
promiseMap: promiseMap
};
if (require.main === module) {
// A micro sanity test.
slowForEach([1, 2, 3], 1000, function(item) {
console.log(item);
if (item === 2) { throw new Error('hate even numbers'); }
}).then(function() {
console.log('Done!');
});
}