You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
516 B
JavaScript
27 lines
516 B
JavaScript
8 years ago
|
'use strict';
|
||
|
|
||
|
var Promise = require("bluebird");
|
||
|
|
||
|
module.exports = function promiseWhile(predicate, func) {
|
||
|
var results = [];
|
||
|
|
||
|
function iteration(value) {
|
||
|
return Promise.try(function () {
|
||
|
return func(value);
|
||
|
}).then(function (result) {
|
||
|
results.push(result);
|
||
|
|
||
|
return Promise.try(function () {
|
||
|
return predicate(result);
|
||
|
}).then(function (predicateResult) {
|
||
|
if (predicateResult) {
|
||
|
return iteration(result);
|
||
|
} else {
|
||
|
return results;
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return iteration();
|
||
|
};
|