Skip to content

Commit

Permalink
Added p.while API
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-mooser committed Apr 12, 2015
1 parent f3b00f4 commit 06649e4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@

global._ = require('lodash');
global.p = require('bluebird');
/**
* Add a while loop to the bluebird promises...
* @param condition function which returns truthy if the action should be called again.
* @param action function to call as the loop implementation.
* @returns {*}
*/
p.while = function(condition, action) {
var resolver = p.defer();

var loop = function() {
if (!condition()) return resolver.resolve();
return p.cast(action())
.then(loop)
.catch(function(err) {
resolver.reject(err);
});
};

process.nextTick(loop);

return resolver.promise;
};

global.path = require('path');
global.fs = require('./kernel/file-storage')();
global.assert = require('assert-plus');
Expand Down

0 comments on commit 06649e4

Please sign in to comment.