Skip to content

Instantly share code, notes, and snippets.

@noamr
Last active March 27, 2024 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamr/88d5c1e33e9558986a9da075ba623d82 to your computer and use it in GitHub Desktop.
Save noamr/88d5c1e33e9558986a9da075ba623d82 to your computer and use it in GitHub Desktop.
Use of `fetchLater` in an async way
const events = [];
let current_dequeue_end = 0;
function dequeue() {
// Ready to process, will send all current events.
current_dequeue_end = events.length;
fetchLater(makeURL(events)).then(({activated}) => {
if (activated) {
// Success, remove processed sub-array from the array and reset the index.
events.splice(0, current_deque_end);
current_dequeue_end = 0;
} else {
// abort the previous fetchLater
}
// If more events were added while processing, process them now.
if (current_dequeue_end != events.length) {
dequeue();
}
}).catch(error => {
// Quota error? handle
});
}
function newEvent(event) {
events.push(event);
if (!current_dequeue_end)
dequeue();
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment