3 Async is a utility module which provides straight-forward, powerful functions
4 for working with asynchronous JavaScript. Although originally designed for
5 use with [node.js](http://nodejs.org), it can also be used directly in the
6 browser. Also supports [component](https://github.com/component/component).
8 Async provides around 20 functions that include the usual 'functional'
9 suspects (map, reduce, filter, each…) as well as some common patterns
10 for asynchronous control flow (parallel, series, waterfall…). All these
11 functions assume you follow the node.js convention of providing a single
12 callback as the last argument of your async function.
18 async.map(['file1','file2','file3'], fs.stat, function(err, results){
19 // results is now an array of stats for each file
22 async.filter(['file1','file2','file3'], fs.exists, function(results){
23 // results now equals an array of the existing files
37 There are many more functions available so take a look at the docs below for a
38 full list. This module aims to be comprehensive, so if you feel anything is
39 missing please create a GitHub issue for it.
43 ### Binding a context to an iterator
45 This section is really about bind, not about async. If you are wondering how to
46 make async execute your iterators in a given context, or are confused as to why
47 a method of another library isn't working as an iterator, study this example:
50 // Here is a simple object with an (unnecessarily roundabout) squaring method
51 var AsyncSquaringLibrary = {
53 square: function(number, callback){
54 var result = Math.pow(number, this.squareExponent);
55 setTimeout(function(){
56 callback(null, result);
61 async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){
62 // result is [NaN, NaN, NaN]
63 // This fails because the `this.squareExponent` expression in the square
64 // function is not evaluated in the context of AsyncSquaringLibrary, and is
65 // therefore undefined.
68 async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), function(err, result){
69 // result is [1, 4, 9]
70 // With the help of bind we can attach a context to the iterator before
71 // passing it to async. Now the square function will be executed in its
72 // 'home' AsyncSquaringLibrary context and the value of `this.squareExponent`
73 // will be as expected.
79 The source is available for download from
80 [GitHub](http://github.com/caolan/async).
81 Alternatively, you can install using Node Package Manager (npm):
85 __Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 29.6kb Uncompressed
89 So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage:
92 <script type="text/javascript" src="async.js"></script>
93 <script type="text/javascript">
95 async.map(data, asyncProcess, function(err, results){
107 * [eachSeries](#eachSeries)
108 * [eachLimit](#eachLimit)
110 * [mapSeries](#mapSeries)
111 * [mapLimit](#mapLimit)
113 * [filterSeries](#filterSeries)
115 * [rejectSeries](#rejectSeries)
117 * [reduceRight](#reduceRight)
119 * [detectSeries](#detectSeries)
124 * [concatSeries](#concatSeries)
129 * [parallel](#parallel)
130 * [parallelLimit](#parallellimittasks-limit-callback)
132 * [doWhilst](#doWhilst)
134 * [doUntil](#doUntil)
135 * [forever](#forever)
136 * [waterfall](#waterfall)
137 * [compose](#compose)
138 * [applyEach](#applyEach)
139 * [applyEachSeries](#applyEachSeries)
143 * [iterator](#iterator)
145 * [nextTick](#nextTick)
147 * [timesSeries](#timesSeries)
151 * [memoize](#memoize)
152 * [unmemoize](#unmemoize)
155 * [noConflict](#noConflict)
162 ### each(arr, iterator, callback)
164 Applies an iterator function to each item in an array, in parallel.
165 The iterator is called with an item from the list and a callback for when it
166 has finished. If the iterator passes an error to this callback, the main
167 callback for the each function is immediately called with the error.
169 Note, that since this function applies the iterator to each item in parallel
170 there is no guarantee that the iterator functions will complete in order.
174 * arr - An array to iterate over.
175 * iterator(item, callback) - A function to apply to each item in the array.
176 The iterator is passed a callback(err) which must be called once it has
177 completed. If no error has occured, the callback should be run without
178 arguments or with an explicit null argument.
179 * callback(err) - A callback which is called after all the iterator functions
180 have finished, or an error has occurred.
185 // assuming openFiles is an array of file names and saveFile is a function
186 // to save the modified contents of that file:
188 async.each(openFiles, saveFile, function(err){
189 // if any of the saves produced an error, err would equal that error
193 ---------------------------------------
195 <a name="forEachSeries" />
196 <a name="eachSeries" />
197 ### eachSeries(arr, iterator, callback)
199 The same as each only the iterator is applied to each item in the array in
200 series. The next iterator is only called once the current one has completed
201 processing. This means the iterator functions will complete in order.
204 ---------------------------------------
206 <a name="forEachLimit" />
207 <a name="eachLimit" />
208 ### eachLimit(arr, limit, iterator, callback)
210 The same as each only no more than "limit" iterators will be simultaneously
213 Note that the items are not processed in batches, so there is no guarantee that
214 the first "limit" iterator functions will complete before any others are
219 * arr - An array to iterate over.
220 * limit - The maximum number of iterators to run at any time.
221 * iterator(item, callback) - A function to apply to each item in the array.
222 The iterator is passed a callback(err) which must be called once it has
223 completed. If no error has occured, the callback should be run without
224 arguments or with an explicit null argument.
225 * callback(err) - A callback which is called after all the iterator functions
226 have finished, or an error has occurred.
231 // Assume documents is an array of JSON objects and requestApi is a
232 // function that interacts with a rate-limited REST api.
234 async.eachLimit(documents, 20, requestApi, function(err){
235 // if any of the saves produced an error, err would equal that error
239 ---------------------------------------
242 ### map(arr, iterator, callback)
244 Produces a new array of values by mapping each value in the given array through
245 the iterator function. The iterator is called with an item from the array and a
246 callback for when it has finished processing. The callback takes 2 arguments,
247 an error and the transformed item from the array. If the iterator passes an
248 error to this callback, the main callback for the map function is immediately
249 called with the error.
251 Note, that since this function applies the iterator to each item in parallel
252 there is no guarantee that the iterator functions will complete in order, however
253 the results array will be in the same order as the original array.
257 * arr - An array to iterate over.
258 * iterator(item, callback) - A function to apply to each item in the array.
259 The iterator is passed a callback(err, transformed) which must be called once
260 it has completed with an error (which can be null) and a transformed item.
261 * callback(err, results) - A callback which is called after all the iterator
262 functions have finished, or an error has occurred. Results is an array of the
263 transformed items from the original array.
268 async.map(['file1','file2','file3'], fs.stat, function(err, results){
269 // results is now an array of stats for each file
273 ---------------------------------------
275 <a name="mapSeries" />
276 ### mapSeries(arr, iterator, callback)
278 The same as map only the iterator is applied to each item in the array in
279 series. The next iterator is only called once the current one has completed
280 processing. The results array will be in the same order as the original.
283 ---------------------------------------
285 <a name="mapLimit" />
286 ### mapLimit(arr, limit, iterator, callback)
288 The same as map only no more than "limit" iterators will be simultaneously
291 Note that the items are not processed in batches, so there is no guarantee that
292 the first "limit" iterator functions will complete before any others are
297 * arr - An array to iterate over.
298 * limit - The maximum number of iterators to run at any time.
299 * iterator(item, callback) - A function to apply to each item in the array.
300 The iterator is passed a callback(err, transformed) which must be called once
301 it has completed with an error (which can be null) and a transformed item.
302 * callback(err, results) - A callback which is called after all the iterator
303 functions have finished, or an error has occurred. Results is an array of the
304 transformed items from the original array.
309 async.mapLimit(['file1','file2','file3'], 1, fs.stat, function(err, results){
310 // results is now an array of stats for each file
314 ---------------------------------------
317 ### filter(arr, iterator, callback)
321 Returns a new array of all the values which pass an async truth test.
322 _The callback for each iterator call only accepts a single argument of true or
323 false, it does not accept an error argument first!_ This is in-line with the
324 way node libraries work with truth tests like fs.exists. This operation is
325 performed in parallel, but the results array will be in the same order as the
330 * arr - An array to iterate over.
331 * iterator(item, callback) - A truth test to apply to each item in the array.
332 The iterator is passed a callback(truthValue) which must be called with a
333 boolean argument once it has completed.
334 * callback(results) - A callback which is called after all the iterator
335 functions have finished.
340 async.filter(['file1','file2','file3'], fs.exists, function(results){
341 // results now equals an array of the existing files
345 ---------------------------------------
347 <a name="filterSeries" />
348 ### filterSeries(arr, iterator, callback)
350 __alias:__ selectSeries
352 The same as filter only the iterator is applied to each item in the array in
353 series. The next iterator is only called once the current one has completed
354 processing. The results array will be in the same order as the original.
356 ---------------------------------------
359 ### reject(arr, iterator, callback)
361 The opposite of filter. Removes values that pass an async truth test.
363 ---------------------------------------
365 <a name="rejectSeries" />
366 ### rejectSeries(arr, iterator, callback)
368 The same as reject, only the iterator is applied to each item in the array
372 ---------------------------------------
375 ### reduce(arr, memo, iterator, callback)
377 __aliases:__ inject, foldl
379 Reduces a list of values into a single value using an async iterator to return
380 each successive step. Memo is the initial state of the reduction. This
381 function only operates in series. For performance reasons, it may make sense to
382 split a call to this function into a parallel map, then use the normal
383 Array.prototype.reduce on the results. This function is for situations where
384 each step in the reduction needs to be async, if you can get the data before
385 reducing it then it's probably a good idea to do so.
389 * arr - An array to iterate over.
390 * memo - The initial state of the reduction.
391 * iterator(memo, item, callback) - A function applied to each item in the
392 array to produce the next step in the reduction. The iterator is passed a
393 callback(err, reduction) which accepts an optional error as its first
394 argument, and the state of the reduction as the second. If an error is
395 passed to the callback, the reduction is stopped and the main callback is
396 immediately called with the error.
397 * callback(err, result) - A callback which is called after all the iterator
398 functions have finished. Result is the reduced value.
403 async.reduce([1,2,3], 0, function(memo, item, callback){
405 process.nextTick(function(){
406 callback(null, memo + item)
408 }, function(err, result){
409 // result is now equal to the last value of memo, which is 6
413 ---------------------------------------
415 <a name="reduceRight" />
416 ### reduceRight(arr, memo, iterator, callback)
420 Same as reduce, only operates on the items in the array in reverse order.
423 ---------------------------------------
426 ### detect(arr, iterator, callback)
428 Returns the first value in a list that passes an async truth test. The
429 iterator is applied in parallel, meaning the first iterator to return true will
430 fire the detect callback with that result. That means the result might not be
431 the first item in the original array (in terms of order) that passes the test.
433 If order within the original array is important then look at detectSeries.
437 * arr - An array to iterate over.
438 * iterator(item, callback) - A truth test to apply to each item in the array.
439 The iterator is passed a callback(truthValue) which must be called with a
440 boolean argument once it has completed.
441 * callback(result) - A callback which is called as soon as any iterator returns
442 true, or after all the iterator functions have finished. Result will be
443 the first item in the array that passes the truth test (iterator) or the
444 value undefined if none passed.
449 async.detect(['file1','file2','file3'], fs.exists, function(result){
450 // result now equals the first file in the list that exists
454 ---------------------------------------
456 <a name="detectSeries" />
457 ### detectSeries(arr, iterator, callback)
459 The same as detect, only the iterator is applied to each item in the array
460 in series. This means the result is always the first in the original array (in
461 terms of array order) that passes the truth test.
464 ---------------------------------------
467 ### sortBy(arr, iterator, callback)
469 Sorts a list by the results of running each value through an async iterator.
473 * arr - An array to iterate over.
474 * iterator(item, callback) - A function to apply to each item in the array.
475 The iterator is passed a callback(err, sortValue) which must be called once it
476 has completed with an error (which can be null) and a value to use as the sort
478 * callback(err, results) - A callback which is called after all the iterator
479 functions have finished, or an error has occurred. Results is the items from
480 the original array sorted by the values returned by the iterator calls.
485 async.sortBy(['file1','file2','file3'], function(file, callback){
486 fs.stat(file, function(err, stats){
487 callback(err, stats.mtime);
489 }, function(err, results){
490 // results is now the original array of files sorted by
495 ---------------------------------------
498 ### some(arr, iterator, callback)
502 Returns true if at least one element in the array satisfies an async test.
503 _The callback for each iterator call only accepts a single argument of true or
504 false, it does not accept an error argument first!_ This is in-line with the
505 way node libraries work with truth tests like fs.exists. Once any iterator
506 call returns true, the main callback is immediately called.
510 * arr - An array to iterate over.
511 * iterator(item, callback) - A truth test to apply to each item in the array.
512 The iterator is passed a callback(truthValue) which must be called with a
513 boolean argument once it has completed.
514 * callback(result) - A callback which is called as soon as any iterator returns
515 true, or after all the iterator functions have finished. Result will be
516 either true or false depending on the values of the async tests.
521 async.some(['file1','file2','file3'], fs.exists, function(result){
522 // if result is true then at least one of the files exists
526 ---------------------------------------
529 ### every(arr, iterator, callback)
533 Returns true if every element in the array satisfies an async test.
534 _The callback for each iterator call only accepts a single argument of true or
535 false, it does not accept an error argument first!_ This is in-line with the
536 way node libraries work with truth tests like fs.exists.
540 * arr - An array to iterate over.
541 * iterator(item, callback) - A truth test to apply to each item in the array.
542 The iterator is passed a callback(truthValue) which must be called with a
543 boolean argument once it has completed.
544 * callback(result) - A callback which is called after all the iterator
545 functions have finished. Result will be either true or false depending on
546 the values of the async tests.
551 async.every(['file1','file2','file3'], fs.exists, function(result){
552 // if result is true then every file exists
556 ---------------------------------------
559 ### concat(arr, iterator, callback)
561 Applies an iterator to each item in a list, concatenating the results. Returns the
562 concatenated list. The iterators are called in parallel, and the results are
563 concatenated as they return. There is no guarantee that the results array will
564 be returned in the original order of the arguments passed to the iterator function.
568 * arr - An array to iterate over
569 * iterator(item, callback) - A function to apply to each item in the array.
570 The iterator is passed a callback(err, results) which must be called once it
571 has completed with an error (which can be null) and an array of results.
572 * callback(err, results) - A callback which is called after all the iterator
573 functions have finished, or an error has occurred. Results is an array containing
574 the concatenated results of the iterator function.
579 async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files){
580 // files is now a list of filenames that exist in the 3 directories
584 ---------------------------------------
586 <a name="concatSeries" />
587 ### concatSeries(arr, iterator, callback)
589 Same as async.concat, but executes in series instead of parallel.
595 ### series(tasks, [callback])
597 Run an array of functions in series, each one running once the previous
598 function has completed. If any functions in the series pass an error to its
599 callback, no more functions are run and the callback for the series is
600 immediately called with the value of the error. Once the tasks have completed,
601 the results are passed to the final callback as an array.
603 It is also possible to use an object instead of an array. Each property will be
604 run as a function and the results will be passed to the final callback as an object
605 instead of an array. This can be a more readable way of handling results from
611 * tasks - An array or object containing functions to run, each function is passed
612 a callback(err, result) it must call on completion with an error (which can
613 be null) and an optional result value.
614 * callback(err, results) - An optional callback to run once all the functions
615 have completed. This function gets a results array (or object) containing all
616 the result arguments passed to the task callbacks.
624 callback(null, 'one');
627 // do some more stuff ...
628 callback(null, 'two');
632 function(err, results){
633 // results is now equal to ['one', 'two']
637 // an example using an object instead of an array
639 one: function(callback){
640 setTimeout(function(){
644 two: function(callback){
645 setTimeout(function(){
650 function(err, results) {
651 // results is now equal to: {one: 1, two: 2}
655 ---------------------------------------
657 <a name="parallel" />
658 ### parallel(tasks, [callback])
660 Run an array of functions in parallel, without waiting until the previous
661 function has completed. If any of the functions pass an error to its
662 callback, the main callback is immediately called with the value of the error.
663 Once the tasks have completed, the results are passed to the final callback as an
666 It is also possible to use an object instead of an array. Each property will be
667 run as a function and the results will be passed to the final callback as an object
668 instead of an array. This can be a more readable way of handling results from
674 * tasks - An array or object containing functions to run, each function is passed
675 a callback(err, result) it must call on completion with an error (which can
676 be null) and an optional result value.
677 * callback(err, results) - An optional callback to run once all the functions
678 have completed. This function gets a results array (or object) containing all
679 the result arguments passed to the task callbacks.
686 setTimeout(function(){
687 callback(null, 'one');
691 setTimeout(function(){
692 callback(null, 'two');
697 function(err, results){
698 // the results array will equal ['one','two'] even though
699 // the second function had a shorter timeout.
703 // an example using an object instead of an array
705 one: function(callback){
706 setTimeout(function(){
710 two: function(callback){
711 setTimeout(function(){
716 function(err, results) {
717 // results is now equals to: {one: 1, two: 2}
721 ---------------------------------------
723 <a name="parallel" />
724 ### parallelLimit(tasks, limit, [callback])
726 The same as parallel only the tasks are executed in parallel with a maximum of "limit"
727 tasks executing at any time.
729 Note that the tasks are not executed in batches, so there is no guarantee that
730 the first "limit" tasks will complete before any others are started.
734 * tasks - An array or object containing functions to run, each function is passed
735 a callback(err, result) it must call on completion with an error (which can
736 be null) and an optional result value.
737 * limit - The maximum number of tasks to run at any time.
738 * callback(err, results) - An optional callback to run once all the functions
739 have completed. This function gets a results array (or object) containing all
740 the result arguments passed to the task callbacks.
742 ---------------------------------------
745 ### whilst(test, fn, callback)
747 Repeatedly call fn, while test returns true. Calls the callback when stopped,
752 * test() - synchronous truth test to perform before each execution of fn.
753 * fn(callback) - A function to call each time the test passes. The function is
754 passed a callback(err) which must be called once it has completed with an
755 optional error argument.
756 * callback(err) - A callback which is called after the test fails and repeated
757 execution of fn has stopped.
765 function () { return count < 5; },
766 function (callback) {
768 setTimeout(callback, 1000);
771 // 5 seconds have passed
776 ---------------------------------------
778 <a name="doWhilst" />
779 ### doWhilst(fn, test, callback)
781 The post check version of whilst. To reflect the difference in the order of operations `test` and `fn` arguments are switched. `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
783 ---------------------------------------
786 ### until(test, fn, callback)
788 Repeatedly call fn, until test returns true. Calls the callback when stopped,
791 The inverse of async.whilst.
793 ---------------------------------------
796 ### doUntil(fn, test, callback)
798 Like doWhilst except the test is inverted. Note the argument ordering differs from `until`.
800 ---------------------------------------
803 ### forever(fn, callback)
805 Calls the asynchronous function 'fn' repeatedly, in series, indefinitely.
806 If an error is passed to fn's callback then 'callback' is called with the
807 error, otherwise it will never be called.
809 ---------------------------------------
811 <a name="waterfall" />
812 ### waterfall(tasks, [callback])
814 Runs an array of functions in series, each passing their results to the next in
815 the array. However, if any of the functions pass an error to the callback, the
816 next function is not executed and the main callback is immediately called with
821 * tasks - An array of functions to run, each function is passed a
822 callback(err, result1, result2, ...) it must call on completion. The first
823 argument is an error (which can be null) and any further arguments will be
824 passed as arguments in order to the next task.
825 * callback(err, [results]) - An optional callback to run once all the functions
826 have completed. This will be passed the results of the last task's callback.
835 callback(null, 'one', 'two');
837 function(arg1, arg2, callback){
838 callback(null, 'three');
840 function(arg1, callback){
841 // arg1 now equals 'three'
842 callback(null, 'done');
844 ], function (err, result) {
845 // result now equals 'done'
849 ---------------------------------------
851 ### compose(fn1, fn2...)
853 Creates a function which is a composition of the passed asynchronous
854 functions. Each function consumes the return value of the function that
855 follows. Composing functions f(), g() and h() would produce the result of
856 f(g(h())), only this version uses callbacks to obtain the return values.
858 Each function is executed with the `this` binding of the composed function.
862 * functions... - the asynchronous functions to compose
868 function add1(n, callback) {
869 setTimeout(function () {
870 callback(null, n + 1);
874 function mul3(n, callback) {
875 setTimeout(function () {
876 callback(null, n * 3);
880 var add1mul3 = async.compose(mul3, add1);
882 add1mul3(4, function (err, result) {
883 // result now equals 15
887 ---------------------------------------
888 <a name="applyEach" />
889 ### applyEach(fns, args..., callback)
891 Applies the provided arguments to each function in the array, calling the
892 callback after all functions have completed. If you only provide the first
893 argument then it will return a function which lets you pass in the
894 arguments as if it were a single function call.
898 * fns - the asynchronous functions to all call with the same arguments
899 * args... - any number of separate arguments to pass to the function
900 * callback - the final argument should be the callback, called when all
901 functions have completed processing
907 async.applyEach([enableSearch, updateSchema], 'bucket', callback);
909 // partial application example:
912 async.applyEach([enableSearch, updateSchema]),
917 ---------------------------------------
919 <a name="applyEachSeries" />
920 ### applyEachSeries(arr, iterator, callback)
922 The same as applyEach only the functions are applied in series.
924 ---------------------------------------
927 ### queue(worker, concurrency)
929 Creates a queue object with the specified concurrency. Tasks added to the
930 queue will be processed in parallel (up to the concurrency limit). If all
931 workers are in progress, the task is queued until one is available. Once
932 a worker has completed a task, the task's callback is called.
936 * worker(task, callback) - An asynchronous function for processing a queued
937 task, which must call its callback(err) argument when finished, with an
938 optional error as an argument.
939 * concurrency - An integer for determining how many worker functions should be
944 The queue object returned by this function has the following properties and
947 * length() - a function returning the number of items waiting to be processed.
948 * concurrency - an integer for determining how many worker functions should be
949 run in parallel. This property can be changed after a queue is created to
950 alter the concurrency on-the-fly.
951 * push(task, [callback]) - add a new task to the queue, the callback is called
952 once the worker has finished processing the task.
953 instead of a single task, an array of tasks can be submitted. the respective callback is used for every task in the list.
954 * unshift(task, [callback]) - add a new task to the front of the queue.
955 * saturated - a callback that is called when the queue length hits the concurrency and further tasks will be queued
956 * empty - a callback that is called when the last item from the queue is given to a worker
957 * drain - a callback that is called when the last item from the queue has returned from the worker
962 // create a queue object with concurrency 2
964 var q = async.queue(function (task, callback) {
965 console.log('hello ' + task.name);
971 q.drain = function() {
972 console.log('all items have been processed');
975 // add some items to the queue
977 q.push({name: 'foo'}, function (err) {
978 console.log('finished processing foo');
980 q.push({name: 'bar'}, function (err) {
981 console.log('finished processing bar');
984 // add some items to the queue (batch-wise)
986 q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) {
987 console.log('finished processing bar');
990 // add some items to the front of the queue
992 q.unshift({name: 'bar'}, function (err) {
993 console.log('finished processing bar');
997 ---------------------------------------
1000 ### cargo(worker, [payload])
1002 Creates a cargo object with the specified payload. Tasks added to the
1003 cargo will be processed altogether (up to the payload limit). If the
1004 worker is in progress, the task is queued until it is available. Once
1005 the worker has completed some tasks, each callback of those tasks is called.
1009 * worker(tasks, callback) - An asynchronous function for processing an array of
1010 queued tasks, which must call its callback(err) argument when finished, with
1011 an optional error as an argument.
1012 * payload - An optional integer for determining how many tasks should be
1013 processed per round; if omitted, the default is unlimited.
1017 The cargo object returned by this function has the following properties and
1020 * length() - a function returning the number of items waiting to be processed.
1021 * payload - an integer for determining how many tasks should be
1022 process per round. This property can be changed after a cargo is created to
1023 alter the payload on-the-fly.
1024 * push(task, [callback]) - add a new task to the queue, the callback is called
1025 once the worker has finished processing the task.
1026 instead of a single task, an array of tasks can be submitted. the respective callback is used for every task in the list.
1027 * saturated - a callback that is called when the queue length hits the concurrency and further tasks will be queued
1028 * empty - a callback that is called when the last item from the queue is given to a worker
1029 * drain - a callback that is called when the last item from the queue has returned from the worker
1034 // create a cargo object with payload 2
1036 var cargo = async.cargo(function (tasks, callback) {
1037 for(var i=0; i<tasks.length; i++){
1038 console.log('hello ' + tasks[i].name);
1046 cargo.push({name: 'foo'}, function (err) {
1047 console.log('finished processing foo');
1049 cargo.push({name: 'bar'}, function (err) {
1050 console.log('finished processing bar');
1052 cargo.push({name: 'baz'}, function (err) {
1053 console.log('finished processing baz');
1057 ---------------------------------------
1060 ### auto(tasks, [callback])
1062 Determines the best order for running functions based on their requirements.
1063 Each function can optionally depend on other functions being completed first,
1064 and each function is run as soon as its requirements are satisfied. If any of
1065 the functions pass an error to their callback, that function will not complete
1066 (so any other functions depending on it will not run) and the main callback
1067 will be called immediately with the error. Functions also receive an object
1068 containing the results of functions which have completed so far.
1070 Note, all functions are called with a results object as a second argument,
1071 so it is unsafe to pass functions in the tasks object which cannot handle the
1072 extra argument. For example, this snippet of code:
1076 readData: async.apply(fs.readFile, 'data.txt', 'utf-8')
1080 will have the effect of calling readFile with the results object as the last
1081 argument, which will fail:
1084 fs.readFile('data.txt', 'utf-8', cb, {});
1087 Instead, wrap the call to readFile in a function which does not forward the
1092 readData: function(cb, results){
1093 fs.readFile('data.txt', 'utf-8', cb);
1100 * tasks - An object literal containing named functions or an array of
1101 requirements, with the function itself the last item in the array. The key
1102 used for each function or array is used when specifying requirements. The
1103 function receives two arguments: (1) a callback(err, result) which must be
1104 called when finished, passing an error (which can be null) and the result of
1105 the function's execution, and (2) a results object, containing the results of
1106 the previously executed functions.
1107 * callback(err, results) - An optional callback which is called when all the
1108 tasks have been completed. The callback will receive an error as an argument
1109 if any tasks pass an error to their callback. Results will always be passed
1110 but if an error occurred, no other tasks will be performed, and the results
1111 object will only contain partial results.
1118 get_data: function(callback){
1119 // async code to get some data
1121 make_folder: function(callback){
1122 // async code to create a directory to store a file in
1123 // this is run at the same time as getting the data
1125 write_file: ['get_data', 'make_folder', function(callback){
1126 // once there is some data and the directory exists,
1127 // write the data to a file in the directory
1128 callback(null, filename);
1130 email_link: ['write_file', function(callback, results){
1131 // once the file is written let's email a link to it...
1132 // results.write_file contains the filename returned by write_file.
1137 This is a fairly trivial example, but to do this using the basic parallel and
1138 series functions would look like this:
1143 // async code to get some data
1146 // async code to create a directory to store a file in
1147 // this is run at the same time as getting the data
1150 function(err, results){
1153 // once there is some data and the directory exists,
1154 // write the data to a file in the directory
1157 // once the file is written let's email a link to it...
1163 For a complicated series of async tasks using the auto function makes adding
1164 new tasks much easier and makes the code more readable.
1167 ---------------------------------------
1169 <a name="iterator" />
1172 Creates an iterator function which calls the next function in the array,
1173 returning a continuation to call the next one after that. It's also possible to
1174 'peek' the next iterator by doing iterator.next().
1176 This function is used internally by the async module but can be useful when
1177 you want to manually control the flow of functions in series.
1181 * tasks - An array of functions to run.
1186 var iterator = async.iterator([
1187 function(){ sys.p('one'); },
1188 function(){ sys.p('two'); },
1189 function(){ sys.p('three'); }
1192 node> var iterator2 = iterator();
1194 node> var iterator3 = iterator2();
1198 node> var nextfn = iterator2.next();
1203 ---------------------------------------
1206 ### apply(function, arguments..)
1208 Creates a continuation function with some arguments already applied, a useful
1209 shorthand when combined with other control flow functions. Any arguments
1210 passed to the returned function are added to the arguments originally passed
1215 * function - The function you want to eventually apply all arguments to.
1216 * arguments... - Any number of arguments to automatically apply when the
1217 continuation is called.
1225 async.apply(fs.writeFile, 'testfile1', 'test1'),
1226 async.apply(fs.writeFile, 'testfile2', 'test2'),
1230 // the same process without using apply
1234 fs.writeFile('testfile1', 'test1', callback);
1237 fs.writeFile('testfile2', 'test2', callback);
1242 It's possible to pass any number of additional arguments when calling the
1246 node> var fn = async.apply(sys.puts, 'one');
1247 node> fn('two', 'three');
1253 ---------------------------------------
1255 <a name="nextTick" />
1256 ### nextTick(callback)
1258 Calls the callback on a later loop around the event loop. In node.js this just
1259 calls process.nextTick, in the browser it falls back to setImmediate(callback)
1260 if available, otherwise setTimeout(callback, 0), which means other higher priority
1261 events may precede the execution of the callback.
1263 This is used internally for browser-compatibility purposes.
1267 * callback - The function to call on a later loop around the event loop.
1272 var call_order = [];
1273 async.nextTick(function(){
1274 call_order.push('two');
1275 // call_order now equals ['one','two']
1277 call_order.push('one')
1281 ### times(n, callback)
1283 Calls the callback n times and accumulates results in the same manner
1284 you would use with async.map.
1288 * n - The number of times to run the function.
1289 * callback - The function to call n times.
1294 // Pretend this is some complicated async factory
1295 var createUser = function(id, callback) {
1301 async.times(5, function(n, next){
1302 createUser(n, function(err, user) {
1305 }, function(err, users) {
1306 // we should now have 5 users
1310 <a name="timesSeries" />
1311 ### timesSeries(n, callback)
1313 The same as times only the iterator is applied to each item in the array in
1314 series. The next iterator is only called once the current one has completed
1315 processing. The results array will be in the same order as the original.
1320 <a name="memoize" />
1321 ### memoize(fn, [hasher])
1323 Caches the results of an async function. When creating a hash to store function
1324 results against, the callback is omitted from the hash and an optional hash
1325 function can be used.
1327 The cache of results is exposed as the `memo` property of the function returned
1332 * fn - the function you to proxy and cache results from.
1333 * hasher - an optional function for generating a custom hash for storing
1334 results, it has all the arguments applied to it apart from the callback, and
1335 must be synchronous.
1340 var slow_fn = function (name, callback) {
1342 callback(null, result);
1344 var fn = async.memoize(slow_fn);
1346 // fn can now be used as if it were slow_fn
1347 fn('some name', function () {
1352 <a name="unmemoize" />
1355 Undoes a memoized function, reverting it to the original, unmemoized
1356 form. Comes handy in tests.
1360 * fn - the memoized function
1363 ### log(function, arguments)
1365 Logs the result of an async function to the console. Only works in node.js or
1366 in browsers that support console.log and console.error (such as FF and Chrome).
1367 If multiple arguments are returned from the async function, console.log is
1368 called on each argument in order.
1372 * function - The function you want to eventually apply all arguments to.
1373 * arguments... - Any number of arguments to apply to the function.
1378 var hello = function(name, callback){
1379 setTimeout(function(){
1380 callback(null, 'hello ' + name);
1385 node> async.log(hello, 'world');
1389 ---------------------------------------
1392 ### dir(function, arguments)
1394 Logs the result of an async function to the console using console.dir to
1395 display the properties of the resulting object. Only works in node.js or
1396 in browsers that support console.dir and console.error (such as FF and Chrome).
1397 If multiple arguments are returned from the async function, console.dir is
1398 called on each argument in order.
1402 * function - The function you want to eventually apply all arguments to.
1403 * arguments... - Any number of arguments to apply to the function.
1408 var hello = function(name, callback){
1409 setTimeout(function(){
1410 callback(null, {hello: name});
1415 node> async.dir(hello, 'world');
1419 ---------------------------------------
1421 <a name="noConflict" />
1424 Changes the value of async back to its original value, returning a reference to the