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.

Async does not protect against array modifications during the function's runtime

See original GitHub issue

When 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:closed
  • Created 9 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ORESoftwarecommented, Jun 25, 2017

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.

0reactions
aearlycommented, Jun 1, 2015

This is a duplicate of #557.

Read more comments on GitHub >

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

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