Skip to content

Commit 7705097

Browse files
authored
fix(scan): return the last item with done:false (#660)
to comfort the iterator protocol
1 parent 3b3e0f3 commit 7705097

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/query.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,14 @@ module.exports = function(AV) {
406406
* @param {number} [options.batchSize] specify the batch size for each request
407407
* @param {AuthOptions} [authOptions]
408408
* @return {AsyncIterator.<AV.Object>}
409-
* @example const scan = new AV.Query(TestClass).scan({
410-
* orderedBy: 'objectId',
411-
* batchSize: 10,
412-
* }, {
413-
* useMasterKey: true,
414-
* });
415-
* const getTen = () => Promise.all(new Array(10).fill(0).map(() => scan.next()));
416-
* getTen().then(results => {
417-
* // results are fisrt 10 instances of TestClass
418-
* return getTen();
419-
* }).then(results => {
420-
* // 11 - 20
421-
* });
409+
* @example const testIterator = {
410+
* [Symbol.asyncIterator]() {
411+
* return new Query('Test').scan(undefined, { useMasterKey: true });
412+
* },
413+
* };
414+
* for await (const test of testIterator) {
415+
* console.log(test.id);
416+
* }
422417
*/
423418
scan({ orderedBy, batchSize } = {}, authOptions) {
424419
const condition = this._getParams();
@@ -445,16 +440,16 @@ module.exports = function(AV) {
445440
if (batchSize) condition.limit = batchSize;
446441
let promise = Promise.resolve([]);
447442
let cursor;
448-
let done = false;
443+
let endReached = false;
449444
return {
450445
next: () => {
451446
promise = promise.then(remainResults => {
452-
if (done) return [];
447+
if (endReached) return [];
453448
if (remainResults.length > 1) return remainResults;
454449
// no cursor means we have reached the end
455450
// except for the first time
456451
if (!cursor && remainResults.length !== 0) {
457-
done = true;
452+
endReached = true;
458453
return remainResults;
459454
}
460455
// when only 1 item left in queue
@@ -472,15 +467,15 @@ module.exports = function(AV) {
472467
return this._parseResponse(response);
473468
})
474469
.then(results => {
475-
if (!results.length) done = true;
470+
if (!results.length) endReached = true;
476471
return remainResults.concat(results);
477472
});
478473
});
479474
return promise
480475
.then(remainResults => remainResults.shift())
481476
.then(result => ({
482477
value: result,
483-
done,
478+
done: result === undefined,
484479
}));
485480
},
486481
};

test/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ describe('Queries', function() {
471471
.then(() => scan.next())
472472
.then(({ value, done }) => {
473473
value.should.be.instanceof(TestClass);
474-
done.should.be.true();
474+
done.should.be.false();
475475
})
476476
.then(() => scan.next())
477477
.then(({ value, done }) => {

0 commit comments

Comments
 (0)