summaryrefslogtreecommitdiffstats
path: root/lib/utils/promise.js
blob: c25b34935b7821733ca9ce3f362c4ad42c9bc71f (plain)
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
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;
        });
    }, []);
}

module.exports = Q;
module.exports.reduce = reduce;
module.exports.serie = serie;