Way to determine unprocessed items from async.map() after error is emitted
See original GitHub issueHi,
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 node request
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 that map
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?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top GitHub Comments
Hi @parky128, thanks for the question!
Would wrapping the
iteratee
withreflect
work?reflect
always passes a result object to thecallback
, so even if one of theiteratee
functions errors,map
would finish. Then you could just iterate over theresults
object frommap
, check which ones have anerror
property, and then handle it accordingly. You wouldn’t have to reprocess any items thatmap
may not have picked up.Otherwise, to answer your question,
map
always returns anarray
. You could iterate over that array and check which values areundefined
. Those will correspond to item(s) that either errored, passedundefined
to theircallback
, were in progress when theerror
occurred or had not started. Thereflect
approach is probably the safer option though, asundefined
may be a valid result from aniteratee
call.@hargasinski - I have ended up using the
async.reflect
approach and this is working nicely for me, gives me full visibility of all items that gave an error 👍Thanks!