JSFiddle - React, Tailwind, and code Playground
HTML
<div id="out"></div>
JavaScript
var printMsg = function(msg) {
var promise = new Promise(function(resolve, reject){
var index = 0;
var out = $('#out').append('<pre></pre>');
var msgOut = setInterval(function() {
out.children(':last-child').append(msg[index++]);
if (index >= msg.length) {
clearInterval(msgOut);
resolve();
};
}, 150);
});
return promise;
}
function printMessages(messages){
if(messages.length){
printMsg(messages[0])
.then(function(){
printMessages(messages.slice(1, messages.length))
});
}
}
var messages = [
'This is the first sentence.',
'This is another sentence.',
'We can do this all day long...'
];
printMessages(messages);