JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://raw.github.com/ricmrodrigues/runasync/master/dist/runasync.js"></script>
<button id="clickme">Click me!</button>
CSS
button {
position: absolute;
left: 0;
top: 0;
}
JavaScript
document.querySelector("button").addEventListener("click", function() {
var start = new Date();
for (var x=1;x<=3;x++) {
(function(idx) {
var task = Task.run(function(idx) {
//dispath something to UI thread
var millisecondsToWait = 500;
setTimeout(function() {
dispatch(function() {
$("body").append("<button>yay....</button>");
});
}, millisecondsToWait);
var response = 0,
max = (89000000*(idx*3)); ;
for (var i=0;i<max;i++) {
i++;
if (i === max - 1) response = i;
}
return response;
}, [idx]);
//execute something after the background task, in the UI thread
task.when(function(value) {
$('#clickme').text("#"+idx+" done!");
console.log("my async code #"+idx+" completed!!");
console.log("output: " + value);
});
//after the previous task ends, spin up a new task
//with its result as argument
task.continueWith(function(value) {
return value;
}).when(function(value) {
console.log("result from task " + value);
});
})(x);
}
$('button').animate({
left: '+=250',
top: '+=200'
}, 5000, function() {
console.log("animation done!");
});
console.log("took " + (new Date(new Date()-start)).getMilliseconds()+ "ms");
}, false);