| 168 | } |
| 169 | |
| 170 | const nextPage = () => { |
| 171 | queryOwner.query(nextParams).then(page => { |
| 172 | let index = 0 |
| 173 | let item |
| 174 | |
| 175 | const next = () => { |
| 176 | if (index >= page.items.length) { |
| 177 | if (page.lastPage) { |
| 178 | done() |
| 179 | } else { |
| 180 | nextParams = page.next |
| 181 | nextPage() |
| 182 | } |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | item = page.items[index] |
| 187 | index++ |
| 188 | |
| 189 | // Pass the next item to the processor, as well as two loop |
| 190 | // operations: |
| 191 | // |
| 192 | // - next(): Continue to next item |
| 193 | // - done(err): Then terminate the loop by fulfilling the outer promise |
| 194 | // |
| 195 | // The process can also terminate the loop by returning a promise |
| 196 | // that will reject. |
| 197 | |
| 198 | let res = processor(item, next, done) |
| 199 | if (res && typeof res.catch === 'function') { |
| 200 | res.catch(reject) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | next() |
| 205 | }).catch(reject) // fail processor loop on query failure |
| 206 | } |
| 207 | |
| 208 | nextPage() |
| 209 | }) |