/
module-ControlFlow.html
6779 lines (2523 loc) · 122 KB
/
module-ControlFlow.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ControlFlow - Documentation</title>
<link rel="icon" href="favicon.ico?v=2">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="styles/prettify-tomorrow.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,700">
<link rel="stylesheet" href="styles/jsdoc-default.css">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/ionicons/2.0.1/css/ionicons.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<a class="navbar-brand" href="../">
<img src="img/async-logo.svg" alt="Async.js">
</a>
</div>
<ul class="nav navbar-nav">
<li id="version-dropdown" class="dropdown">
<a href="#" class="dropdown-toggle vertically-centered" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">v3.0.1 <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="../v3/">v3.0.x</a></li>
<li><a href="../v2/">v2.6.2</a></li>
<li>
<a href="https://github.com/caolan/async/blob/v1.5.2/README.md">v1.5.x</a>
</li>
</ul>
</li>
<li><a href="./index.html">Home</a></li>
<li><a href="./docs.html">Docs</a></li>
<li><a href="https://github.com/caolan/async/blob/master/CHANGELOG.md">Changelog</a></li>
<li><a href="https://github.com/caolan/async"><i class="ion-social-github" aria-hidden="true"></i></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control typeahead" id="doc-search" placeholder="Search" autofocus>
</div>
</form>
</ul>
</div>
<input type="checkbox" id="nav-trigger" class="nav-trigger">
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<div id="main">
<div id="main-container" data-spy="scroll" data-target="#toc" data-offset="50">
<h1 class="page-title">Control Flow</h1>
<section>
<header>
</header>
<article>
<div class="container-overview">
<div class="description"><p>A collection of <code>async</code> functions for controlling the flow through a script.</p></div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line56">line 56</a>
</li></ul></dd>
</dl>
</div>
<h3 class="subsection-title">Methods</h3>
<h4 class="name" id="applyEach"><span class="type-signature">(static) </span>applyEach<span class="signature">(fns, …args<span class="signature-attributes">opt</span>, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {<a href="global.html">AsyncFunction</a>}</span></h4>
<div class="description">
<pre class="prettyprint source"><code>import applyEach from 'async/applyEach';</code></pre><p>Applies the provided arguments to each function in the array, calling
<code>callback</code> after all functions have completed. If you only provide the first
argument, <code>fns</code>, then it will return a function which lets you pass in the
arguments as if it were a single function call. If more arguments are
provided, <code>callback</code> is required while <code>args</code> is still optional. The results
for each of the applied async functions are passed to the final callback
as an array.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>fns</code></td>
<td class="type">
<span class="param-type">Array</span>
|
<span class="param-type">Iterable</span>
|
<span class="param-type">AsyncIterable</span>
|
<span class="param-type">Object</span>
</td>
<td class="description last"><p>A collection of <a href="global.html">AsyncFunction</a>s
to all call with the same arguments</p></td>
</tr>
<tr>
<td class="name"><code>args</code></td>
<td class="type">
<span class="param-type">*</span>
<span class="optional"><optional></span>
</td>
<td class="description last"><p>any number of separate arguments to pass to the
function.</p></td>
</tr>
<tr>
<td class="name"><code>callback</code></td>
<td class="type">
<span class="param-type">function</span>
<span class="optional"><optional></span>
</td>
<td class="description last"><p>the final argument should be the callback,
called when all functions have completed processing.</p></td>
</tr>
</tbody>
</table>
<h5>Returns:</h5>
<div class="param-desc">
<ul>
<li>Returns a function that takes no args other than
an optional callback, that is the result of applying the <code>args</code> to each
of the functions.</li>
</ul>
</div>
<dl class="param-type">
<dt>
Type
</dt>
<dd>
<span class="param-type"><a href="global.html">AsyncFunction</a></span>
</dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>const appliedFn = async.applyEach([enableSearch, updateSchema], 'bucket')
appliedFn((err, results) => {
// results[0] is the results for `enableSearch`
// results[1] is the results for `updateSchema`
});
// partial application example:
async.each(
buckets,
async (bucket) => async.applyEach([enableSearch, updateSchema], bucket)(),
callback
);</code></pre>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="applyEach.js.html">applyEach.js</a>, <a href="applyEach.js.html#line4">line 4</a>
</li></ul></dd>
</dl>
<h4 class="name" id="applyEachSeries"><span class="type-signature">(static) </span>applyEachSeries<span class="signature">(fns, …args<span class="signature-attributes">opt</span>, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {<a href="global.html">AsyncFunction</a>}</span></h4>
<div class="description">
<pre class="prettyprint source"><code>import applyEachSeries from 'async/applyEachSeries';</code></pre><p>The same as <a href="docs.html#applyEach"><code>applyEach</code></a> but runs only a single async operation at a time.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>fns</code></td>
<td class="type">
<span class="param-type">Array</span>
|
<span class="param-type">Iterable</span>
|
<span class="param-type">AsyncIterable</span>
|
<span class="param-type">Object</span>
</td>
<td class="description last"><p>A collection of <a href="global.html">AsyncFunction</a>s to all
call with the same arguments</p></td>
</tr>
<tr>
<td class="name"><code>args</code></td>
<td class="type">
<span class="param-type">*</span>
<span class="optional"><optional></span>
</td>
<td class="description last"><p>any number of separate arguments to pass to the
function.</p></td>
</tr>
<tr>
<td class="name"><code>callback</code></td>
<td class="type">
<span class="param-type">function</span>
<span class="optional"><optional></span>
</td>
<td class="description last"><p>the final argument should be the callback,
called when all functions have completed processing.</p></td>
</tr>
</tbody>
</table>
<h5>Returns:</h5>
<div class="param-desc">
<ul>
<li>A function, that when called, is the result of
appling the <code>args</code> to the list of functions. It takes no args, other than
a callback.</li>
</ul>
</div>
<dl class="param-type">
<dt>
Type
</dt>
<dd>
<span class="param-type"><a href="global.html">AsyncFunction</a></span>
</dd>
</dl>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="applyEachSeries.js.html">applyEachSeries.js</a>, <a href="applyEachSeries.js.html#line4">line 4</a>
</li></ul></dd>
<dt class="tag-see">See:</dt>
<dd class="tag-see">
<ul>
<li><p><a href="docs.html#applyEach">async.applyEach</a></p></li>
</ul>
</dd>
</dl>
<h4 class="name" id="auto"><span class="type-signature">(static) </span>auto<span class="signature">(tasks, concurrency<span class="signature-attributes">opt</span>, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise}</span></h4>
<div class="description">
<pre class="prettyprint source"><code>import auto from 'async/auto';</code></pre><p>Determines the best order for running the <a href="global.html">AsyncFunction</a>s in <code>tasks</code>, based on
their requirements. Each function can optionally depend on other functions
being completed first, and each function is run as soon as its requirements
are satisfied.</p>
<p>If any of the <a href="global.html">AsyncFunction</a>s pass an error to their callback, the <code>auto</code> sequence
will stop. Further tasks will not execute (so any other functions depending
on it will not run), and the main <code>callback</code> is immediately called with the
error.</p>
<p><a href="global.html">AsyncFunction</a>s also receive an object containing the results of functions which
have completed so far as the first argument, if they have dependencies. If a
task function has no dependencies, it will only be passed a callback.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>tasks</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="default">
</td>
<td class="description last"><p>An object. Each of its properties is either a
function or an array of requirements, with the <a href="global.html">AsyncFunction</a> itself the last item
in the array. The object's key of a property serves as the name of the task
defined by that property, i.e. can be used when specifying requirements for
other tasks. The function receives one or two arguments:</p>
<ul>
<li>a <code>results</code> object, containing the results of the previously executed
functions, only passed if the task has any dependencies,</li>
<li>a <code>callback(err, result)</code> function, which must be called when finished,
passing an <code>error</code> (which can be <code>null</code>) and the result of the function's
execution.</li>
</ul></td>
</tr>
<tr>
<td class="name"><code>concurrency</code></td>
<td class="type">
<span class="param-type">number</span>
<span class="optional"><optional></span>
</td>
<td class="default">
Infinity
</td>
<td class="description last"><p>An optional <code>integer</code> for
determining the maximum number of tasks that can be run in parallel. By
default, as many as possible.</p></td>
</tr>
<tr>
<td class="name"><code>callback</code></td>
<td class="type">
<span class="param-type">function</span>
<span class="optional"><optional></span>
</td>
<td class="default">
</td>
<td class="description last"><p>An optional callback which is called when all
the tasks have been completed. It receives the <code>err</code> argument if any <code>tasks</code>
pass an error to their callback. Results are always returned; however, if an
error occurs, no further <code>tasks</code> will be performed, and the results object
will only contain partial results. Invoked with (err, results).</p></td>
</tr>
</tbody>
</table>
<h5>Returns:</h5>
<div class="param-desc">
<p>a promise, if a callback is not passed</p>
</div>
<dl class="param-type">
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>async.auto({
// this function will just be passed a callback
readData: async.apply(fs.readFile, 'data.txt', 'utf-8'),
showData: ['readData', function(results, cb) {
// results.readData is the file's contents
// ...
}]
}, callback);
async.auto({
get_data: function(callback) {
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback) {
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(results, callback) {
console.log('in write_file', JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(results, callback) {
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'user@example.com'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});</code></pre>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="auto.js.html">auto.js</a>, <a href="auto.js.html#line6">line 6</a>
</li></ul></dd>
</dl>
<h4 class="name" id="autoInject"><span class="type-signature">(static) </span>autoInject<span class="signature">(tasks, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise}</span></h4>
<div class="description">
<pre class="prettyprint source"><code>import autoInject from 'async/autoInject';</code></pre><p>A dependency-injected version of the <a href="docs.html#auto">async.auto</a> function. Dependent
tasks are specified as parameters to the function, after the usual callback
parameter, with the parameter names matching the names of the tasks it
depends on. This can provide even more readable task graphs which can be
easier to maintain.</p>
<p>If a final callback is specified, the task results are similarly injected,
specified as named parameters after the initial error parameter.</p>
<p>The autoInject function is purely syntactic sugar and its semantics are
otherwise equivalent to <a href="docs.html#auto">async.auto</a>.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>