Skip to content

Question about spider version 3 example in the Asynchronous Control Flow Patterns with Callbacks #70

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

Open
tarrsalah opened this issue Feb 21, 2023 · 0 comments

Comments

@tarrsalah
Copy link

In the Web spider version 3 code example (Asynchronous Control Flow Patterns with Callbacks) chapter, The author defined a done callback (will be called asynchronously by the spiderLinks function).

function done (err) {
  if (err) {
    hasErrors = true
    return cb(err)
  }
  if (++completed === links.length && !hasErrors) {
    return cb()
  }
}

The author mentioned that:

The hasErrors variable is necessary because if one parallel task
fails, we want to immediately call the callback with the given error.
Also, we need to make sure that other parallel tasks that might still
be running won't invoke the callback again.

I read done definition multiple times, and think that the !hasErrors check is not need in the ++completed === links.length && !hasErrors) because if we have at least one error (in one of the concurrent functions) ++completed will not be reached in that function, so the completed === links.length will never be satisfied. so probably we need to check !hasErrors in:

  if (err && !hasErrors) {
    hasErrors = true
    return cb(err)
  }

To avoid calling the callback with error multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant