JSFiddle - React, Tailwind, and code Playground

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.9.26/bluebird.js"></script>

JavaScript

function whileLoop(condition, fn) {
    return Promise.
    try (function loop(val) {
        return Promise.resolve(condition()).then(function (res) {
            if (!res) return val; // done
            return fn().then(loop); // keep on looping
        });
    });
}

var log = function (msg) {
    document.body.innerHTML += msg + "<br />";
};

log("Before loop body");
var i = 0;
whileLoop(function () {
    return i < 10; // can also return a promise for async here
}, function body() {
    log("In loop body, and i is: "+ i);
    i++;
    return Promise.delay(1000);
}).then(function () {
    log("All done!");
});