Why do I need Async Collections?
Me: So I want to do async operations on a batch of items...
Async: Say no more, bro.
So, aync collection library have 12 distinct groups of functions, each including what I call a base form and its derivatives. The 12 base functions are:
concat, detect,each, every, filter, groupBy, map, sortBy, reduce, transform, some, reject.
A little bit too much? Men's memory block is 4-7, right? Don't worry. Let's group them.
Let's do it in the form of a little questionare.
1. So you have a batch of items on which you want to run async functions. Is the function a truth test?
A. Truth test. (Go to 2) B. NO, OF COURSE NOT YOU ANIMAL. (Go to 5)
2. Do you just want a yes or no as result, or you want to have those objects as results?
A. Gimme boolean.(Go to 3) B. Gimme Objects.(Go to 4)
3. So you want a simple yes or no. You wanna to know if every one of the items have passed the test? Or just if any of them have passed the test?
A. Every. (Use every.) B. Any. (Use some)
4. You want group of items as results. Do you want the first one that has passed the test, or all? Or maybe all that failed the test?
A. First one passed.(Use detect). B. All passed.(Use filter). C.All failed. (Use reject.)
5. So you dont want truth test. But maybe you want a retained state between operation on each object?
A. NO, Seriously why would I want that.(Go to 6). B. YES. (Go to 9)
6.So you want normal stuff. Ok, do you need a grouped result of all operations or you are ok if they are seperately handled?
A. Separate, please. (Use each.) B. No. I need all results to be handled together. (Go to 7)
7. So you want results of all items together. Does the sequence matter to you?
A. YES(Go to 8). B. NO.(Use concat)
8. So you want results in right sequence. Do you have a criteria to group them, sort them, or original sequence of item is fine?
A. Group. (Use groupBy). B.Sort. (Use sortBy) C. Original sequence.(Use map)
9. So you want a retained state between operations. You can do it in series, or do it in parallel but with a key information. Which one?
A. Series. (Use reduce). B.With key. (Use transform)
To sum up, let's use a chart.
I also made a small test program to tell the difference between each, concat and map.
And here's the result.
Here's a list of all the 'affixes'.
- series : The async operation is run in series. One at a time.
- limit : Runs a maximum number of async operations at a time. User can define this number through argument 'limit'.
- of : This is only used with 'each'. Let you supply key as an arguement of the async ooperations.
- values: Used with 'map' function only. This function is used for objects and the async operation can have a key argument
- right :Only used with reduce. Run series of items in reverse order.
Limit | Series | |
---|---|---|
concat | ✓ | |
detect | ✓ | ✓ |
every | ✓ | ✓ |
filter | ✓ | ✓ |
groupBy | ✓ | ✓ |
reduce | ||
reject | ✓ | ✓ |
some | ✓ | ✓ |
each | ✓ | ✓ |
sortBy | ||
map | ✓ | ✓ |
transform |
Special cases with each, map, reduce
each: eachOf, eachOfLimits. eachOfSeries
map: mapValues, mapValuesLimit, mapValuesSeries
reduce: reduceRight