eachSeries should finish if underlying array is modified
See original GitHub issueWhat version of async are you using? 2.2.0
Which environment did the issue occur in (Node version/browser version) nodejs 7.7.3
What did you do? Please include a minimal reproducable case illustrating issue.
it("async.eachSeries : add an element in underlying array while iterating should finish iteration, per docs", function(testDone) {
var data = [1, 2, 3, 4, 5];
var results = [];
var iterator = function(collection, item, eachDone) {
console.log("we process item =" + item);
results.push(item);
if (item === 2) {
// we add "new" after 2
collection.splice(2, 0, "new");
}
return eachDone();
};
async.eachSeries(data, iterator.bind(null, data), function(err) {
expect(err).to.be.not.exist;
// we expect the iteration to finish after have processed value 2, but actually, the iteration continues, but with respect to the original array length
expect(results).to.have.length(2);
testDone();
});
});
What did you expect to happen?
According to changelog v1.10 : eachSeries and family will finish if the underlying array is modified during execution (#557) I did not find more recent reference to this topic.
So I expected the iteration to stop after have modified the array
What was the actual result?
The iterations continue, but only according to the original length of the array
FWIW, I need to add elements (and so iterations) to the array while iterating, that’s why I was testing this behaviour.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6
Top Results From Across the Web
6.3. Enhanced For-Loop (For-Each) for Arrays
Use the enhanced for each loop with arrays whenever you can, because it cuts down on errors. You can use it whenever you...
Read more >Chapter 7: Arrays - cs.utsa.edu
Write a program that declares a double array of length 4, prints the values, assigns a value to each element, and prints the...
Read more >Final Arrays in Java - GeeksforGeeks
Output Explanation: The array arr is declared as final, but the elements of an array are changed without any problem.
Read more >For Each...Next Statement - Visual Basic - Microsoft Learn
Next Statement works well when you can associate each iteration of a loop with a control ... For more examples, see Collections and...
Read more >Array methods - The Modern JavaScript Tutorial
When we need to iterate over an array – we can use forEach , for or for..of . When we need to iterate...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ah yes queue seems a good candidate for my use case, thx 👍 . Though shouldn’t the docs be updated to warn about the immutability of the collection ? I dont’ find any reference to this, eg in http://caolan.github.io/async/docs.html#each, except the changelog of old v1.10 I mentioned.
If you are open to a PR for the docs, where in the docs do you think it would be the most relevant ?
@nakiabrewer
doWhilst
orqueue
might be better suited to your task. (Iterate whilebody.data.next_page
exists, or keep pushing new pages to the queue.)