kevva / github-repositories

Get all GitHub repos from a user or an organization
MIT License
37 stars 11 forks source link

Promisify #7

Closed kevva closed 8 years ago

kevva commented 8 years ago

@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 8 years ago

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 8 years ago

@SamVerschueren Yes, that's exactly what I was thinking too.

kevva commented 8 years ago

Yeah, must have done something wrong when I tried it earlier :(. Thanks anyway!