1
1
# Async
2
2
_ Higher-order functions and common patterns for asynchronous code in node.js_
3
3
4
- I've so far avoided using the exising async modules in favour of the standard
4
+ I've so far avoided using the existing async modules in favour of the standard
5
5
callbacks provided by node. When writing modules, I find sticking to the
6
6
convention of using a single callback makes the API easier to understand, and
7
7
allows people to wrap the module with other methods of handling async code if
@@ -32,11 +32,17 @@ alternative modules like node-continuables.
32
32
in the given array through an async iterator function.
33
33
* __ filter (filterSeries)__ - Returns a new array of all the values which pass
34
34
an async truth test.
35
- * __ reduce__ - Reduces a list of values into a single value using an async
36
- iterator to return each successive step.
35
+ * __ reject (rejectSeries)__ - The opposite of filter, removes items that
36
+ passes an async test.
37
+ * __ reduce (reduceRight)__ - Reduces a list of values into a single value
38
+ using an async iterator to return each successive step.
39
+ * __ detect (detectSeries)__ - Returns the first value is a list that passes an
40
+ async truth test.
41
+ * __ sortBy__ - Sorts a list by the results of running each value through an
42
+ async iterator, leaving the original values intact.
37
43
* __ some__ - Returns true if at least one element in the array satisfies an
38
44
async test.
39
- * __ every__ - Returns true if evert element in the array satisfies an async
45
+ * __ every__ - Returns true if every element in the array satisfies an async
40
46
test.
41
47
42
48
### Flow Control
@@ -127,6 +133,8 @@ processing. The results array will be in the same order as the original.
127
133
128
134
### filter(arr, iterator, callback)
129
135
136
+ __ Alias:__ select
137
+
130
138
Returns a new array of all the values which pass an async truth test.
131
139
_ The callback for each iterator call only accepts a single argument of true or
132
140
false, it does not accept an error argument first!_ This is inline with the
@@ -150,13 +158,26 @@ __Example__
150
158
151
159
### filterSeries(arr, iterator, callback)
152
160
161
+ __ alias:__ selectSeries
162
+
153
163
The same as filter only the iterator is applied to each item in the array in
154
164
series. The next iterator is only called once the current one has completed
155
165
processing. The results array will be in the same order as the original.
156
166
167
+ ### reject(arr, iterator, callback)
168
+
169
+ The opposite of filter. Removes values that pass an async truth test.
170
+
171
+ ### rejectSeries(arr, iterator, callback)
172
+
173
+ The same as filter, only the iterator is applied to each item in the array
174
+ in series.
175
+
157
176
158
177
### reduce(arr, memo, iterator, callback)
159
178
179
+ __ aliases:__ inject, foldl
180
+
160
181
Reduces a list of values into a single value using an async iterator to return
161
182
each successive step. Memo is the initial state of the reduction. This
162
183
function only operates in series. For performance reasons, it may make sense to
@@ -189,7 +210,74 @@ __Example__
189
210
// result is now equal to the last value of memo, which is 6
190
211
});
191
212
192
- ### some
213
+ ### reduceRight(arr, memo, iterator, callback)
214
+
215
+ __ Alias:__ foldr
216
+
217
+ Same as reduce, only operates on the items in the array in reverse order.
218
+
219
+
220
+ ### detect(arr, iterator, callback)
221
+
222
+ Returns the first value in a list that passes an async truth test. The
223
+ iterator is applied in parallel, meaning the first iterator to return true will
224
+ fire the detect callback with that result. That means the result might not be
225
+ the first item in the original array (in terms of order) that passes the test.
226
+
227
+ If order within the original array is important then look at detectSeries.
228
+
229
+ __ Arguments__
230
+
231
+ * arr - An array to iterator over.
232
+ * iterator(item, callback) - A truth test to apply to each item in the array.
233
+ The iterator is passed a callback which must be called once it has completed.
234
+ * callback(result) - A callback which is called as soon as any iterator returns
235
+ true, or after all the iterator functions have finished. Result will be
236
+ the first item in the array that passes the truth test (iterator) or the
237
+ value undefined if none passed.
238
+
239
+ __ Example__
240
+
241
+ async.detect(['file1','file2','file3'], path.exists, function(result){
242
+ // result now equals the first file in the list that exists
243
+ });
244
+
245
+ ### detectSeries(arr, iterator, callback)
246
+
247
+ The same as detect, only the iterator is applied to each item in the array
248
+ in series. This means the result is always the first in the original array (in
249
+ terms of array order) that passes the truth test.
250
+
251
+
252
+ ### sortBy(arr, iterator, callback)
253
+
254
+ Sorts a list by the results of running each value through an async iterator.
255
+
256
+ __ Arguments__
257
+
258
+ * arr - An array to iterate over.
259
+ * iterator(item, callback) - A function to apply to each item in the array.
260
+ The iterator is passed a callback which must be called once it has completed
261
+ with an error (which can be null) and a value to use as the sort criteria.
262
+ * callback(err, results) - A callback which is called after all the iterator
263
+ functions have finished, or an error has occurred. Results is the items from
264
+ the original array sorted by the values returned by the iterator calls.
265
+
266
+ __ Example__
267
+
268
+ async.sortBy(['file1','file2','file3'], function(file, callback){
269
+ fs.stat(file, function(err, stats){
270
+ callback(err, stats.mtime);
271
+ });
272
+ }, function(err, results){
273
+ // results is now the original array of files sorted by
274
+ // modified date
275
+ });
276
+
277
+
278
+ ### some(arr, iterator, callback)
279
+
280
+ __ Alias:__ any
193
281
194
282
Returns true if at least one element in the array satisfies an async test.
195
283
_ The callback for each iterator call only accepts a single argument of true or
@@ -212,9 +300,11 @@ __Example__
212
300
// if result is true then at least one of the files exists
213
301
});
214
302
215
- ### every
303
+ ### every(arr, iterator, callback)
304
+
305
+ __ Alias:__ all
216
306
217
- Returns true if evert element in the array satisfies an async test.
307
+ Returns true if every element in the array satisfies an async test.
218
308
_ The callback for each iterator call only accepts a single argument of true or
219
309
false, it does not accept an error argument first!_ This is inline with the
220
310
way node libraries work with truth tests like path.exists.
0 commit comments