@@ -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+ callbackErrorOrThrow ( cb , path ) ( err ) ;
272+ }
271273 }
272274}
273275
@@ -283,6 +285,7 @@ function methodHasNoBody(method) {
283285
284286function 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 ( / r e l = " n e x t " / ) !== - 1 ) {
288291 return ( link . match ( / < ( .* ) > / ) || [ ] ) [ 1 ] ;
0 commit comments