Skip to content

Instantly share code, notes, and snippets.

@lrecknagel
Forked from getify/1.js
Created September 12, 2019 07:33
Show Gist options
  • Save lrecknagel/689b4f7d114279fd655bd44d63662c24 to your computer and use it in GitHub Desktop.
Save lrecknagel/689b4f7d114279fd655bd44d63662c24 to your computer and use it in GitHub Desktop.
quick sketch of CAF
btn.addEventListener("send-btn",onSend);
send = CAF(send);
var prevSend = Promise.resolve();
function onSend(evt) {
prevSend.finally(function f(){
prevSend = send(
CAF.timeout(1000,"send took too long."),
{ some: "data" }
)
.catch(console.log);
});
}
function *send(signal,data){
var res = yield fetch("/some/url",{
body: JSON.stringify(data),
signal
});
if (res.ok) {
let resp = yield res.text(); // note: this call doesn't need a Promise.race() because CAF does it for you
console.log(resp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment