Async does not protect against array modifications during the function's runtime
See original GitHub issueWhen you have an asynchronous call eg. async.each
, and during that runtime, the array which is passed in has modifications to it, then it may never finish, or will call the callback twice.
Example:
async = require "async"
arr = [1, 2, 3]
async.each arr, ((i, cb) -> console.log "i"; setImmediate(cb)), (err) -> console.log "done"
arr.push(4)
This example loops through the 3 original array elements, and prints i
but then never calls the callback, because in async.each
it’s doing:
if (completed >= arr.length) {
callback(null);
}
When looking at the async code, it’s doing a comparison against arr.length
which can change…would it not be better to store the original array length and do a comparison against that, ensuring that the completed callback will be called?
Fiddle: http://jsfiddle.net/4ysKX/1/
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
async.each callback could be called more than once ... - GitHub
aearly mentioned this issue on May 31, 2015. Async does not protect against array modifications during the function's runtime #572.
Read more >await is only valid in async function - Stack Overflow
For whatever reason, the await keyword has been implemented such that it can only be used within an async method. This is in...
Read more >Guide for running C# Azure Functions in an isolated worker ...
Learn how to use a .NET isolated worker process to run your C# functions in Azure, which supports non-LTS versions of .NET and...
Read more >UpdateFunctionConfiguration - AWS Lambda
During this time, you can't modify the function, but you can still invoke it. The LastUpdateStatus , LastUpdateStatusReason , and LastUpdateStatusReasonCode ...
Read more >Strict mode - JavaScript - MDN Web Docs
Browsers not supporting strict mode will run strict mode code with ... or at runtime); changes simplifying how variable references are ...
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
Did async end up disallowing array modifications after the fact? I have a use case where it would be nice to modify the original array after the fact, so as to iterate on more elements than initially.
This is a duplicate of #557.