Skip to content

Instantly share code, notes, and snippets.

@getify
Last active September 19, 2019 08:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save getify/6ac8e321d0ea50d7d55f8491304f5693 to your computer and use it in GitHub Desktop.
Save getify/6ac8e321d0ea50d7d55f8491304f5693 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