Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Created June 3, 2020 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dfkaye/c9e073f7c539259a0ffab4658f47a909 to your computer and use it in GitHub Desktop.
Save dfkaye/c9e073f7c539259a0ffab4658f47a909 to your computer and use it in GitHub Desktop.
q&d fetch-gists from github
// 3 June 2020
// memorializing this helper to track my too numerous gists
function fetchGists({ url, per_page, page, fn }) {
console.group(`fetch ${ url } at ${ performance.now() }`);
// url must be at least a string
if (Object(url).toString() !== url) {
console.error("param url should be a string");
return console.groupEnd();
}
// set default printout fn for deserialized json results
typeof fn == 'function' || (fn = (o) => { console.log(o.html_url, o.description); });
fetch(`${ url }?per_page=${ per_page || 25 }&page=${ page || 1 }`)
.then(res => res.json())
.then(res => res.map(fn))
.finally(function() { console.groupEnd(); });
}
/* test it out */
var url = 'https://api.github.com/users/dfkaye/gists';
var per_page = 100;
var page = 3;
var fn = (o) => { console.log(o.html_url, o.description); };
fetchGists({ });
fetchGists({ url });
fetchGists({ url, fn });
fetchGists({ url, per_page, fn });
fetchGists({ url, per_page, page, fn });
/* should see nested call groups except the first which logs param error */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment