Created
June 3, 2020 22:39
-
-
Save dfkaye/c9e073f7c539259a0ffab4658f47a909 to your computer and use it in GitHub Desktop.
q&d fetch-gists from github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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