JSFiddle - React, Tailwind, and code Playground
by Norlihazmey Ghazali
HTML
<div id='output'></div>
JavaScript
function log(message) {
var d = new Date();
$('#output').append('<div>' + d.getSeconds() + '.' + d.getMilliseconds() + ': ' + message + '</div>');
}
function asyncWait(millis) {
var dfd = $.Deferred();
setTimeout(function () {
var d = new Date();
log('done waiting for ' + millis + 'ms');
dfd.resolve(millis);
}, millis);
return dfd.promise();
}
function startTest0() {
return asyncWait(1000).then(asyncWait).then(asyncWait).then(asyncWait).done(function () {
log('all done, 4 times');
});
}
function startTest() {
return asyncWait(500).then(function () {
asyncWait(1000);
return 1500;
}).then(function (ms) {
asyncWait(ms);
return 2000;
}).then(function (ms) {
// asyncWait(ms)
return asyncWait(ms)
}).done(function () {
log('all done');
});
}
log('welcome');
log('starting test ...');
startTest0().done(function() { log('starting the second test'); startTest(); });