Question about spider version 3 example in the Asynchronous Control Flow Patterns with Callbacks
tarrsalah opened this issue · 0 comments
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.