Skip to content

Instantly share code, notes, and snippets.

@AnasAboreeda
Created September 17, 2017 12:03
Show Gist options
  • Save AnasAboreeda/595d2dec1911ac0295bd2dd5f08c60cd to your computer and use it in GitHub Desktop.
Save AnasAboreeda/595d2dec1911ac0295bd2dd5f08c60cd to your computer and use it in GitHub Desktop.
[While Promise] A while promise that returns a promise
// Promise while that return promise
var P = require('bluebird');
var promiseWhile = P.method(function(condition, action) {
if (!condition()) {return;}
return action().then(promiseWhile.bind(null, condition, action));
});
/*
Usage:
*/
var condition = true;
promiseWhile(function() { return condition; }, function() {
// some promise logic that will set condition to false at some point;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment