26 lines
521 B
JavaScript
26 lines
521 B
JavaScript
|
|
|
|
exports.seq = function(items, generator) {
|
|
|
|
var results = [];
|
|
var p = Promise.resolve();
|
|
|
|
for(var i = 0, len = items.length; i < len; ++i) {
|
|
p = p.then(generateNextHandler(items[i], i === 0));
|
|
}
|
|
|
|
return p.then(function(lastResult) {
|
|
results.push(lastResult);
|
|
return results;
|
|
});
|
|
|
|
// Internal helper
|
|
function generateNextHandler(item, ignoreResult) {
|
|
return function(prevResult) {
|
|
if(!ignoreResult) results.push(prevResult);
|
|
return generator(item, prevResult);
|
|
};
|
|
}
|
|
|
|
};
|