Skip to content

Commit 4f5a7f1

Browse files
author
James Haggerty
committed
Stop iteration on Request.eachitem returning false
This echoes the behaviour of eachPage.
1 parent 1deb7f1 commit 4f5a7f1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "feature",
3+
"category": "Request",
4+
"description": "eachItem method stops iteration on returning false (like eachPage)"
5+
}

lib/request.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,14 @@ AWS.Request = inherit({
513513
var resultKey = config.resultKey;
514514
if (Array.isArray(resultKey)) resultKey = resultKey[0];
515515
var items = jmespath.search(data, resultKey);
516-
AWS.util.arrayEach(items, function(item) { callback(null, item); });
516+
var continueIteration = true;
517+
AWS.util.arrayEach(items, function(item) {
518+
continueIteration = callback(null, item);
519+
if (continueIteration === false) {
520+
return AWS.util.abort;
521+
}
522+
});
523+
return continueIteration;
517524
}
518525

519526
this.eachPage(wrappedCallback);

test/request.spec.coffee

+17
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ describe 'AWS.Request', ->
192192
]
193193
done()
194194

195+
it 'supports stopping responses if false is returned', ->
196+
helpers.mockResponses [
197+
{data: {Results: [{Value: 1}, {Value: 2}], NextToken: 'a'}},
198+
{data: {Results: [{Value: 3}], NextToken: 'b'}},
199+
{data: Results: [{Value: 4}, {Value: 5}]}
200+
]
201+
resps = []
202+
service.mockMethod().eachItem (err, data) ->
203+
if resps.length == 2
204+
return false
205+
resps.push([err, data])
206+
207+
expect(resps).to.eql [
208+
[null, {Value: 1}],
209+
[null, {Value: 2}]
210+
]
211+
195212
it 'passes error to callback', (done) ->
196213
helpers.mockResponses [
197214
{data: {Results: [{Value: 1}, {Value: 2}], NextToken: 'a'}},

0 commit comments

Comments
 (0)