question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

eachSeries should finish if underlying array is modified

See original GitHub issue

What 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:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
abenhamdinecommented, Apr 1, 2017

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 ?

0reactions
aearlycommented, Aug 8, 2017

@nakiabrewer doWhilst or queue might be better suited to your task. (Iterate while body.data.next_page exists, or keep pushing new pages to the queue.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found