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
Way to determine unprocessed items from async.map() after error is emitted #1449
Comments
Hi @parky128, thanks for the question! Would wrapping the async.map(coll, async.reflect(function(val, callback) {
// your request code
}, function(err, results) {
// err will always be null now
results.forEach(function(result, i) {
if (result.error) {
// your code for handling errors
// if `coll` is an array, you could access the value that caused
// this error through `coll[i]` as `map` preserves the order for arrays
} else {
// otherwise `result.value` will contain the result of that `iteratee` call
}
});
}); Otherwise, to answer your question, |
Thanks for taking the time to offer a potential solution. I would actually want the async.map final callback to get invoked as soon as one of the Looking at the docs I see I can achieve this by passing an error to the iteratee callback function, but I'm wondering if in the final callback that async.map will invoke, whether I could just use that results object to compare with the original collection and see what is left to process. I just dont want async.map to attempt processing any other requests as soon as one of these returns the error case I am interested in. |
Assuming your You could try For example, if your collection is an array async.mapSeries(coll, function(val, callback) {
// your iteratee function
}, function(err, results) {
if (err) {
// unprocessItems will include the item that errored.
var unprocessItems = coll.slice(results.length - 1);
// handle the unprocessedItems
}
}); |
Hmm Ok, yes my iteratee function is asynchronous as it uses the request library to make an outgoing http request and I callback from it with the result. So if I say had 10 items to iterate over, 4 succeeded and the 5th failed which I would then invoke the callback to the iteratee function with an error param, should that mean the results object in the final callback to async.map will contain only the 4 successful results? If it does then for now I will live with the fact it will still make all the outgoing calls. I may down the line be a me to split my array into smaller arrays and perform smaller async.maps within a mapSeries to minimise the initial hit to the target server the requests are hitting. |
@hargasinski - I have ended up using the Thanks! |
Hi,
Whilst using map(...) is there a way when handling an error emitted from the iteratee function to determine how many items have not yet been processed from the collection supplied?
I am using
map
to perform multiple outgoing HTTP requests (using the noderequest
library) for an array of different urls\params, etc. Part way through making any of these requests I may get a particular error from the target server which I can handle, but I then want to re-process the current item being worked on and then any remaining ones thatmap
has not yet picked up.I did wonder about maybe setting a flag on each item in my collection that has been worked on successfully (without error), and then when the error gets emitted that I am interested in, handle it accordingly. Then perhaps create a new array from the items with the flag set to false for those not yet processed and perform a further
map
over these, making sure I invoke the original final callback from the original map.Not sure if this makes any sense, but is there any way to achieve what I have described above?
The text was updated successfully, but these errors were encountered: