Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Expand Up @@ -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()) {
Copy link
Contributor

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:

if (matchesSomething(item)) {

async,nextTick(function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

. instead of a ,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Down