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
|
var Q = require('q');
var _ = require('lodash');
// Reduce an array to a promise
function reduce(arr, iter, base) {
return _.reduce(arr, function(prev, elem, i) {
return prev.then(function(val) {
return iter(val, elem, i);
});
}, Q(base));
}
// Transform an array
function serie(arr, iter, base) {
return reduce(arr, function(before, item, i) {
return Q(iter(item, i))
.then(function(r) {
before.push(r);
return before;
});
}, []);
}
// Iter over an array and return first result (not null)
function some(arr, iter) {
return _.reduce(arr, function(prev, elem, i) {
return prev.then(function(val) {
if (val) return val;
return iter(elem, i);
});
}, Q());
}
// Map an array using an async (promised) iterator
function map(arr, iter) {
return reduce(arr, function(prev, entry, i) {
return Q(iter(entry, i))
.then(function(out) {
prev.push(out);
return prev;
});
}, []);
}
// Wrap a fucntion in a promise
function wrap(func) {
return _.wrap(func, function(_func) {
var args = Array.prototype.slice.call(arguments, 1);
return Q()
.then(function() {
return _func.apply(null, args);
});
});
}
module.exports = Q;
module.exports.reduce = reduce;
module.exports.map = map;
module.exports.serie = serie;
module.exports.some = some;
module.exports.wrapfn = wrap;
|