@@ -235,15 +235,18 @@ class Requestable {
235
235
* @param {string } path - the path to request
236
236
* @param {Object } options - the query parameters to include
237
237
* @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.
239
238
* @return {Promise } - a promise which will resolve when all pages have been fetched
240
239
* @deprecated This will be folded into {@link Requestable#_request} in the 2.0 release.
241
240
*/
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 ) ;
244
249
245
- return this . _request ( 'GET' , path , options )
246
- . then ( ( response ) => {
247
250
let thisGroup ;
248
251
if ( response . data instanceof Array ) {
249
252
thisGroup = response . data ;
@@ -255,19 +258,18 @@ class Requestable {
255
258
}
256
259
results . push ( ...thisGroup ) ;
257
260
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 ) ;
263
263
264
- if ( cb ) {
265
- cb ( null , results , response ) ;
266
- }
264
+ if ( cb ) {
265
+ cb ( null , results , response ) ;
266
+ }
267
267
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
+ }
271
273
}
272
274
}
273
275
@@ -283,6 +285,7 @@ function methodHasNoBody(method) {
283
285
284
286
function getNextPage ( linksHeader = '' ) {
285
287
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
286
289
return links . reduce ( function ( nextUrl , link ) {
287
290
if ( link . search ( / r e l = " n e x t " / ) !== - 1 ) {
288
291
return ( link . match ( / < ( .* ) > / ) || [ ] ) [ 1 ] ;
0 commit comments