New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removing internal setImmediate's #704
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3adc38e
working on removing deferral from auto
aearly bd9d033
fixed all auto tests
9cd8a77
removed deferrals from queue, subtle breaking changes
72fc263
cleaned up priorityQueue and cargo
987e8c2
added notes in README about synchronous iterators and deferring callb…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,42 @@ missing please create a GitHub issue for it. | |
|
||
## Common Pitfalls | ||
|
||
<a name="stack-overflow"> | ||
### Synchronous iteration functions | ||
|
||
If you get an error like `RangeError: Maximum call stack size exceeded.` or other stack overflow issues when using async, you are likely using a synchronous iterator. By *synchronous* we mean a function that calls its callback on the same tick in the javascript event loop, without doing any I/O or using any timers. Calling many callbacks iteratively will quickly overflow the stack. If you run into this issue, just defer your callback with `async.nextTick` to start a new call stack on the next tick of the event loop. | ||
|
||
This can also arise by accident if you callback early in certain cases: | ||
|
||
```js | ||
async.eachSeries(hugeArray, function iterator(item, callback) { | ||
if (matchesSomething(item)) { | ||
callback(null); // if many items match this, you'll overflow | ||
} else { | ||
doSomeIO(item, callback); | ||
} | ||
}, function done() { | ||
//... | ||
}); | ||
``` | ||
|
||
Just change it to: | ||
|
||
```js | ||
async.eachSeries(hugeArray, function iterator(item, callback) { | ||
if (item.somePredicate()) { | ||
async,nextTick(function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were fixed in the version that made it to master: b96705a |
||
callback(null); | ||
}); | ||
} else { | ||
doSomeIO(item, callback); | ||
//... | ||
``` | ||
|
||
Async does not guard against synchronous functions because it adds a small performance hit for each function call. Functions that are asynchronous by their nature do not have this problem and don't need the automatic callback deferral. | ||
|
||
If javascript's event loop is still a bit nebulous, check out [this article](http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/) or [this talk](http://2014.jsconf.eu/speakers/philip-roberts-what-the-heck-is-the-event-loop-anyway.html) for more detailed information about how it works. | ||
|
||
### Binding a context to an iterator | ||
|
||
This section is really about `bind`, not about `async`. If you are wondering how to | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to match the example above, I think this should be changed to: