Skip to content

Instantly share code, notes, and snippets.

@mrsharpoblunto
Created September 6, 2019 16:05
Show Gist options
  • Save mrsharpoblunto/096675904ea7aecca6304be63b700db0 to your computer and use it in GitHub Desktop.
Save mrsharpoblunto/096675904ea7aecca6304be63b700db0 to your computer and use it in GitHub Desktop.
function queryAPI(path) {
const cacheEntry = window.__data[path];
if (!cacheEntry) {
// issue a normal XHR API request
return fetch(path);
} else if (cacheEntry.data) {
// the server has pushed us the data already
return Promise.resolve(cacheEntry.data);
} else {
// the server is still pushing the data
// so we'll put ourselves in the queue to
// be notified when its ready
const waiting = {};
cacheEntry.waiting.push(waiting);
return new Promise((resolve) => {
waiting.resolve = resolve;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment