Promisify
Closed this issue · 3 comments
kevva commented
@SamVerschueren, any suggestions on how to promisify this the best way? I've searched some for recursive promises but couldn't find anything that didn't involve a lot of wrapping/nesting.
SamVerschueren commented
My best bet is something like this.
function getRepos(user, opts) {
var page = 1;
var ret = [];
var url = 'users/' + user + '/repos?&per_page=100&page=' + page;
return (function loop() {
return ghGot(url, opts).then(function (res) {
ret = ret.concat(res.data);
if (res.headers.link && res.headers.link.indexOf('next') !== -1) {
return loop();
}
return ret;
});
}());
}
Not sure about res.data
. Not that familiar with got
. Will try it out tomorrow if I have more time.
sindresorhus commented
@SamVerschueren Yes, that's exactly what I was thinking too.
kevva commented
Yeah, must have done something wrong when I tried it earlier :(. Thanks anyway!