Skip to content

Commit 3a69f64

Browse files
author
Caolan McMahon
committed
Updated README to include detect, reject, reduceRight and sortBy functions, as well as alias information
1 parent 9ca19c1 commit 3a69f64

File tree

1 file changed

+97
-7
lines changed

1 file changed

+97
-7
lines changed

README.md

+97-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Async
22
_Higher-order functions and common patterns for asynchronous code in node.js_
33

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
55
callbacks provided by node. When writing modules, I find sticking to the
66
convention of using a single callback makes the API easier to understand, and
77
allows people to wrap the module with other methods of handling async code if
@@ -32,11 +32,17 @@ alternative modules like node-continuables.
3232
in the given array through an async iterator function.
3333
* __filter (filterSeries)__ - Returns a new array of all the values which pass
3434
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.
3743
* __some__ - Returns true if at least one element in the array satisfies an
3844
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
4046
test.
4147

4248
### Flow Control
@@ -127,6 +133,8 @@ processing. The results array will be in the same order as the original.
127133

128134
### filter(arr, iterator, callback)
129135

136+
__Alias:__ select
137+
130138
Returns a new array of all the values which pass an async truth test.
131139
_The callback for each iterator call only accepts a single argument of true or
132140
false, it does not accept an error argument first!_ This is inline with the
@@ -150,13 +158,26 @@ __Example__
150158

151159
### filterSeries(arr, iterator, callback)
152160

161+
__alias:__ selectSeries
162+
153163
The same as filter only the iterator is applied to each item in the array in
154164
series. The next iterator is only called once the current one has completed
155165
processing. The results array will be in the same order as the original.
156166

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+
157176

158177
### reduce(arr, memo, iterator, callback)
159178

179+
__aliases:__ inject, foldl
180+
160181
Reduces a list of values into a single value using an async iterator to return
161182
each successive step. Memo is the initial state of the reduction. This
162183
function only operates in series. For performance reasons, it may make sense to
@@ -189,7 +210,74 @@ __Example__
189210
// result is now equal to the last value of memo, which is 6
190211
});
191212

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
193281

194282
Returns true if at least one element in the array satisfies an async test.
195283
_The callback for each iterator call only accepts a single argument of true or
@@ -212,9 +300,11 @@ __Example__
212300
// if result is true then at least one of the files exists
213301
});
214302

215-
### every
303+
### every(arr, iterator, callback)
304+
305+
__Alias:__ all
216306

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.
218308
_The callback for each iterator call only accepts a single argument of true or
219309
false, it does not accept an error argument first!_ This is inline with the
220310
way node libraries work with truth tests like path.exists.

0 commit comments

Comments
 (0)