JSFiddle - React, Tailwind, and code Playground
Babel + JSX
let t = 0
setInterval(async function() {
t = t + 1
console.log("timer: ", t)
}, 1000)
async function syncDelay(ms) {
console.log("syncDelay: ", ms, "ms")
let t1 = Date.now()
while (Date.now() - t1 < ms) {}
return ms
}
class WorkManager {
addWork(work) {
if (this.first === undefined) {
this.first = work
this.last = work
this.runWork()
} else {
this.last.next = work
this.last = work
}
}
newWork(fn, ...args) {
this.addWork({fn, args})
}
runWork() {
let work = this.first
if (this.first === undefined) {
return
}
setTimeout(() => {
this.first = this.first.next
work.fn(...work.args)
this.runWork()
}, 0)
}
}
let wm = new WorkManager()
for (let i = 0; i < 10; i++) {
wm.newWork(syncDelay, 500)
}
console.log("after calling heavyWork")