Skip to content

Commit 957282f

Browse files
committed
Cleanup page requests to use async / await
1 parent 03914f2 commit 957282f

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

lib/Requestable.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,18 @@ class Requestable {
235235
* @param {string} path - the path to request
236236
* @param {Object} options - the query parameters to include
237237
* @param {Requestable.callback} [cb] - the function to receive the data. The returned data will always be an array.
238-
* @param {Object[]} results - the partial results. This argument is intended for interal use only.
239238
* @return {Promise} - a promise which will resolve when all pages have been fetched
240239
* @deprecated This will be folded into {@link Requestable#_request} in the 2.0 release.
241240
*/
242-
_requestAllPages(path, options, cb, results) {
243-
results = results || [];
241+
async _requestAllPages(path, options, cb) {
242+
let currentPath = path;
243+
let results = [];
244+
let response;
245+
246+
try {
247+
do {
248+
response = await this._request('GET', currentPath, options);
244249

245-
return this._request('GET', path, options)
246-
.then((response) => {
247250
let thisGroup;
248251
if (response.data instanceof Array) {
249252
thisGroup = response.data;
@@ -255,19 +258,18 @@ class Requestable {
255258
}
256259
results.push(...thisGroup);
257260

258-
const nextUrl = getNextPage(response.headers.link);
259-
if (nextUrl && typeof options.page !== 'number') {
260-
log(`getting next page: ${nextUrl}`);
261-
return this._requestAllPages(nextUrl, options, cb, results);
262-
}
261+
currentPath = getNextPage(response.headers.link);
262+
} while(currentPath);
263263

264-
if (cb) {
265-
cb(null, results, response);
266-
}
264+
if (cb) {
265+
cb(null, results, response);
266+
}
267267

268-
response.data = results;
269-
return response;
270-
}).catch(callbackErrorOrThrow(cb, path));
268+
response.data = results;
269+
return response;
270+
} catch (err) {
271+
return callbackErrorOrThrow(cb, path);
272+
}
271273
}
272274
}
273275

@@ -283,6 +285,7 @@ function methodHasNoBody(method) {
283285

284286
function getNextPage(linksHeader = '') {
285287
const links = linksHeader.split(/\s*,\s*/); // splits and strips the urls
288+
// TODO: Change this to early abort once it finds the link in question
286289
return links.reduce(function(nextUrl, link) {
287290
if (link.search(/rel="next"/) !== -1) {
288291
return (link.match(/<(.*)>/) || [])[1];

test/organization.spec.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ describe('Organization', function() {
2121
});
2222

2323
after(function() {
24-
return github.getProject(createdProject.id).deleteProject();
24+
if (createdProject) {
25+
return github.getProject(createdProject.id).deleteProject();
26+
}
2527
});
2628

2729
describe('reading', function() {
@@ -95,13 +97,10 @@ describe('Organization', function() {
9597
}));
9698
});
9799

98-
it('should list the teams in the organization', function() {
99-
return organization.getTeams()
100-
.then(({data}) => {
101-
const hasTeam = data.some((member) => member.slug === testRepoName);
102-
103-
expect(hasTeam).to.be.true();
104-
});
100+
it('should list the teams in the organization', async function() {
101+
const {data} = await organization.getTeams();
102+
const hasTeam = data.some((member) => member.slug === testRepoName);
103+
expect(hasTeam).to.be.true();
105104
});
106105

107106
it('should create a project', function(done) {

0 commit comments

Comments
 (0)