Skip to content

Commit 159a119

Browse files
romanbalayanRoman Balayan
and
Roman Balayan
authored
Enhance examples for Collections methods. (#1726)
* Enhance examples for Collections methods. * Update collection example code, avoiding usage of IIFE on async/await examples * Convert examples on async.auto, async.series, and async.parallel to samples using Promises and async/await Co-authored-by: Roman Balayan <[email protected]>
1 parent 89255fe commit 159a119

40 files changed

+5267
-637
lines changed

docs/v3/auto.js.html

+77-11
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,40 @@ <h1 class="page-title">auto.js</h1>
121121
* @returns {Promise} a promise, if a callback is not passed
122122
* @example
123123
*
124+
* //Using Callbacks
124125
* async.auto({
125-
* // this function will just be passed a callback
126-
* readData: async.apply(fs.readFile, &apos;data.txt&apos;, &apos;utf-8&apos;),
127-
* showData: [&apos;readData&apos;, function(results, cb) {
128-
* // results.readData is the file&apos;s contents
129-
* // ...
126+
* get_data: function(callback) {
127+
* // async code to get some data
128+
* callback(null, &apos;data&apos;, &apos;converted to array&apos;);
129+
* },
130+
* make_folder: function(callback) {
131+
* // async code to create a directory to store a file in
132+
* // this is run at the same time as getting the data
133+
* callback(null, &apos;folder&apos;);
134+
* },
135+
* write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
136+
* // once there is some data and the directory exists,
137+
* // write the data to a file in the directory
138+
* callback(null, &apos;filename&apos;);
139+
* }],
140+
* email_link: [&apos;write_file&apos;, function(results, callback) {
141+
* // once the file is written let&apos;s email a link to it...
142+
* callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;[email protected]&apos;});
130143
* }]
131-
* }, callback);
144+
* }, function(err, results) {
145+
* if (err) {
146+
* console.log(&apos;err = &apos;, err);
147+
* }
148+
* console.log(&apos;results = &apos;, results);
149+
* // results = {
150+
* // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
151+
* // make_folder; &apos;folder&apos;,
152+
* // write_file: &apos;filename&apos;
153+
* // email_link: { file: &apos;filename&apos;, email: &apos;[email protected]&apos; }
154+
* // }
155+
* });
132156
*
157+
* //Using Promises
133158
* async.auto({
134159
* get_data: function(callback) {
135160
* console.log(&apos;in get_data&apos;);
@@ -143,21 +168,62 @@ <h1 class="page-title">auto.js</h1>
143168
* callback(null, &apos;folder&apos;);
144169
* },
145170
* write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
146-
* console.log(&apos;in write_file&apos;, JSON.stringify(results));
147171
* // once there is some data and the directory exists,
148172
* // write the data to a file in the directory
149173
* callback(null, &apos;filename&apos;);
150174
* }],
151175
* email_link: [&apos;write_file&apos;, function(results, callback) {
152-
* console.log(&apos;in email_link&apos;, JSON.stringify(results));
153176
* // once the file is written let&apos;s email a link to it...
154-
* // results.write_file contains the filename returned by write_file.
155177
* callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;[email protected]&apos;});
156178
* }]
157-
* }, function(err, results) {
158-
* console.log(&apos;err = &apos;, err);
179+
* }).then(results =&gt; {
159180
* console.log(&apos;results = &apos;, results);
181+
* // results = {
182+
* // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
183+
* // make_folder; &apos;folder&apos;,
184+
* // write_file: &apos;filename&apos;
185+
* // email_link: { file: &apos;filename&apos;, email: &apos;[email protected]&apos; }
186+
* // }
187+
* }).catch(err =&gt; {
188+
* console.log(&apos;err = &apos;, err);
160189
* });
190+
*
191+
* //Using async/await
192+
* async () =&gt; {
193+
* try {
194+
* let results = await async.auto({
195+
* get_data: function(callback) {
196+
* // async code to get some data
197+
* callback(null, &apos;data&apos;, &apos;converted to array&apos;);
198+
* },
199+
* make_folder: function(callback) {
200+
* // async code to create a directory to store a file in
201+
* // this is run at the same time as getting the data
202+
* callback(null, &apos;folder&apos;);
203+
* },
204+
* write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
205+
* // once there is some data and the directory exists,
206+
* // write the data to a file in the directory
207+
* callback(null, &apos;filename&apos;);
208+
* }],
209+
* email_link: [&apos;write_file&apos;, function(results, callback) {
210+
* // once the file is written let&apos;s email a link to it...
211+
* callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;[email protected]&apos;});
212+
* }]
213+
* });
214+
* console.log(&apos;results = &apos;, results);
215+
* // results = {
216+
* // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
217+
* // make_folder; &apos;folder&apos;,
218+
* // write_file: &apos;filename&apos;
219+
* // email_link: { file: &apos;filename&apos;, email: &apos;[email protected]&apos; }
220+
* // }
221+
* }
222+
* catch (err) {
223+
* console.log(err);
224+
* }
225+
* }
226+
*
161227
*/
162228
export default function auto(tasks, concurrency, callback) {
163229
if (typeof concurrency !== &apos;number&apos;) {

docs/v3/concat.js.html

+70-2
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,77 @@ <h1 class="page-title">concat.js</h1>
100100
* @returns A Promise, if no callback is passed
101101
* @example
102102
*
103-
* async.concat([&apos;dir1&apos;,&apos;dir2&apos;,&apos;dir3&apos;], fs.readdir, function(err, files) {
104-
* // files is now a list of filenames that exist in the 3 directories
103+
* // dir1 is a directory that contains file1.txt, file2.txt
104+
* // dir2 is a directory that contains file3.txt, file4.txt
105+
* // dir3 is a directory that contains file5.txt
106+
* // dir4 does not exist
107+
*
108+
* let directoryList = [&apos;dir1&apos;,&apos;dir2&apos;,&apos;dir3&apos;];
109+
* let withMissingDirectoryList = [&apos;dir1&apos;,&apos;dir2&apos;,&apos;dir3&apos;, &apos;dir4&apos;];
110+
*
111+
* // Using callbacks
112+
* async.concat(directoryList, fs.readdir, function(err, results) {
113+
* if (err) {
114+
* console.log(err);
115+
* } else {
116+
* console.log(results);
117+
* // [ &apos;file1.txt&apos;, &apos;file2.txt&apos;, &apos;file3.txt&apos;, &apos;file4.txt&apos;, file5.txt ]
118+
* }
119+
* });
120+
*
121+
* // Error Handling
122+
* async.concat(withMissingDirectoryList, fs.readdir, function(err, results) {
123+
* if (err) {
124+
* console.log(err);
125+
* // [ Error: ENOENT: no such file or directory ]
126+
* // since dir4 does not exist
127+
* } else {
128+
* console.log(results);
129+
* }
130+
* });
131+
*
132+
* // Using Promises
133+
* async.concat(directoryList, fs.readdir)
134+
* .then(results =&gt; {
135+
* console.log(results);
136+
* // [ &apos;file1.txt&apos;, &apos;file2.txt&apos;, &apos;file3.txt&apos;, &apos;file4.txt&apos;, file5.txt ]
137+
* }).catch(err =&gt; {
138+
* console.log(err);
105139
* });
140+
*
141+
* // Error Handling
142+
* async.concat(withMissingDirectoryList, fs.readdir)
143+
* .then(results =&gt; {
144+
* console.log(results);
145+
* }).catch(err =&gt; {
146+
* console.log(err);
147+
* // [ Error: ENOENT: no such file or directory ]
148+
* // since dir4 does not exist
149+
* });
150+
*
151+
* // Using async/await
152+
* async () =&gt; {
153+
* try {
154+
* let results = await async.concat(directoryList, fs.readdir);
155+
* console.log(results);
156+
* // [ &apos;file1.txt&apos;, &apos;file2.txt&apos;, &apos;file3.txt&apos;, &apos;file4.txt&apos;, file5.txt ]
157+
* } catch (err) {
158+
* console.log(err);
159+
* }
160+
* }
161+
*
162+
* // Error Handling
163+
* async () =&gt; {
164+
* try {
165+
* let results = await async.concat(withMissingDirectoryList, fs.readdir);
166+
* console.log(results);
167+
* } catch (err) {
168+
* console.log(err);
169+
* // [ Error: ENOENT: no such file or directory ]
170+
* // since dir4 does not exist
171+
* }
172+
* }
173+
*
106174
*/
107175
function concat(coll, iteratee, callback) {
108176
return concatLimit(coll, Infinity, iteratee, callback)

docs/v3/detect.js.html

+40-5
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,48 @@ <h1 class="page-title">detect.js</h1>
107107
* @returns A Promise, if no callback is passed
108108
* @example
109109
*
110-
* async.detect([&apos;file1&apos;,&apos;file2&apos;,&apos;file3&apos;], function(filePath, callback) {
111-
* fs.access(filePath, function(err) {
112-
* callback(null, !err)
113-
* });
114-
* }, function(err, result) {
110+
* // dir1 is a directory that contains file1.txt, file2.txt
111+
* // dir2 is a directory that contains file3.txt, file4.txt
112+
* // dir3 is a directory that contains file5.txt
113+
*
114+
* // asynchronous function that checks if a file exists
115+
* function fileExists(file, callback) {
116+
* fs.access(file, fs.constants.F_OK, (err) =&gt; {
117+
* callback(null, !err);
118+
* });
119+
* }
120+
*
121+
* async.detect([&apos;file3.txt&apos;,&apos;file2.txt&apos;,&apos;dir1/file1.txt&apos;], fileExists,
122+
* function(err, result) {
123+
* console.log(result);
124+
* // dir1/file1.txt
125+
* // result now equals the first file in the list that exists
126+
* }
127+
*);
128+
*
129+
* // Using Promises
130+
* async.detect([&apos;file3.txt&apos;,&apos;file2.txt&apos;,&apos;dir1/file1.txt&apos;], fileExists)
131+
* .then(result =&gt; {
132+
* console.log(result);
133+
* // dir1/file1.txt
115134
* // result now equals the first file in the list that exists
135+
* }).catch(err =&gt; {
136+
* console.log(err);
116137
* });
138+
*
139+
* // Using async/await
140+
* async () =&gt; {
141+
* try {
142+
* let result = await async.detect([&apos;file3.txt&apos;,&apos;file2.txt&apos;,&apos;dir1/file1.txt&apos;], fileExists);
143+
* console.log(result);
144+
* // dir1/file1.txt
145+
* // result now equals the file in the list that exists
146+
* }
147+
* catch (err) {
148+
* console.log(err);
149+
* }
150+
* }
151+
*
117152
*/
118153
function detect(coll, iteratee, callback) {
119154
return createTester(bool =&gt; bool, (res, item) =&gt; item)(eachOf, coll, iteratee, callback)

0 commit comments

Comments
 (0)