JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.js"></script>
JavaScript
function promiseWhile(condition, body) {
var done = RSVP.defer();
function loop() {
// When the result of calling `condition` is no longer true, we are
// done.
if (!condition()) return done.resolve();
// Use `when`, in case `body` does not return a promise.
// When it completes loop again otherwise, if it fails, reject the
// done promise
body().then(loop,done.reject);
}
// Start running the loop in the next tick so that this function is
// completely async. It would be unexpected if `body` was called
// synchronously the first time.
RSVP.async(loop);
// The promise
return done.promise;
}
/*
* Asynchronously decrementing a variable
* The stylish route.
*/
var i = 10;
promiseWhile(function(){
return i > 0;
},function(){
return new RSVP.Promise(function(resolve, reject){
setTimeout(function(){
i--;alert(i);
resolve();
},200);
});
}).then(function(){
alert('done');
});