JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.js"></script>
JavaScript
function promiseWhile(condition, body) {
return new RSVP.Promise(function(resolve,reject){
function loop() {
RSVP.Promise.resolve(condition()).then(function(result){
// When the result of calling `condition` is no longer true, we are done.
if (!result){
resolve();
} else {
// When it completes loop again otherwise, if it fails, reject
RSVP.Promise.resolve(body()).then(loop,reject);
}
});
}
// Start running the loop
loop();
});
}
/*
* 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');
});