JSFiddle - React, Tailwind, and code Playground
by gianlucaguarini
HTML
<script src="https://cdn.rawgit.com/GianlucaGuarini/ruit/master/index.js"></script>
<button id='btn'>
click me
</button>
JavaScript
function ajaxRequest() {
let timer
const p = new Promise((r) => {
timer = setTimeout(r, 500, {
foo: 'foo'
})
})
return {
then: p.then.bind(p),
catch: p.catch.bind(p),
cancel() {
console.log('cancel')
clearTimeout(timer)
}
}
}
const queue = []
function enqueue(job) {
if (queue.length) queue.shift().cancel()
queue.push(job)
return job
}
function doSomeAsync() {
const req = enqueue(ajaxRequest())
ruit(req, dispatch).then(() => console.log('done'))
}
function dispatch(result) {
console.log(result)
return result
}
btn.onclick = doSomeAsync