Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance examples for Collections methods. #1726

Merged
merged 3 commits into from Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 77 additions & 11 deletions docs/v3/auto.js.html
Expand Up @@ -121,15 +121,40 @@ <h1 class="page-title">auto.js</h1>
* @returns {Promise} a promise, if a callback is not passed
* @example
*
* //Using Callbacks
* async.auto({
* // this function will just be passed a callback
* readData: async.apply(fs.readFile, &apos;data.txt&apos;, &apos;utf-8&apos;),
* showData: [&apos;readData&apos;, function(results, cb) {
* // results.readData is the file&apos;s contents
* // ...
* get_data: function(callback) {
* // async code to get some data
* callback(null, &apos;data&apos;, &apos;converted to array&apos;);
* },
* make_folder: function(callback) {
* // async code to create a directory to store a file in
* // this is run at the same time as getting the data
* callback(null, &apos;folder&apos;);
* },
* write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
* // once there is some data and the directory exists,
* // write the data to a file in the directory
* callback(null, &apos;filename&apos;);
* }],
* email_link: [&apos;write_file&apos;, function(results, callback) {
* // once the file is written let&apos;s email a link to it...
* callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;user@example.com&apos;});
* }]
* }, callback);
* }, function(err, results) {
* if (err) {
* console.log(&apos;err = &apos;, err);
* }
* console.log(&apos;results = &apos;, results);
* // results = {
* // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
* // make_folder; &apos;folder&apos;,
* // write_file: &apos;filename&apos;
* // email_link: { file: &apos;filename&apos;, email: &apos;user@example.com&apos; }
* // }
* });
*
* //Using Promises
* async.auto({
* get_data: function(callback) {
* console.log(&apos;in get_data&apos;);
Expand All @@ -143,21 +168,62 @@ <h1 class="page-title">auto.js</h1>
* callback(null, &apos;folder&apos;);
* },
* write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
* console.log(&apos;in write_file&apos;, JSON.stringify(results));
* // once there is some data and the directory exists,
* // write the data to a file in the directory
* callback(null, &apos;filename&apos;);
* }],
* email_link: [&apos;write_file&apos;, function(results, callback) {
* console.log(&apos;in email_link&apos;, JSON.stringify(results));
* // once the file is written let&apos;s email a link to it...
* // results.write_file contains the filename returned by write_file.
* callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;user@example.com&apos;});
* }]
* }, function(err, results) {
* console.log(&apos;err = &apos;, err);
* }).then(results =&gt; {
* console.log(&apos;results = &apos;, results);
* // results = {
* // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
* // make_folder; &apos;folder&apos;,
* // write_file: &apos;filename&apos;
* // email_link: { file: &apos;filename&apos;, email: &apos;user@example.com&apos; }
* // }
* }).catch(err =&gt; {
* console.log(&apos;err = &apos;, err);
* });
*
* //Using async/await
* async () =&gt; {
* try {
* let results = await async.auto({
* get_data: function(callback) {
* // async code to get some data
* callback(null, &apos;data&apos;, &apos;converted to array&apos;);
* },
* make_folder: function(callback) {
* // async code to create a directory to store a file in
* // this is run at the same time as getting the data
* callback(null, &apos;folder&apos;);
* },
* write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
* // once there is some data and the directory exists,
* // write the data to a file in the directory
* callback(null, &apos;filename&apos;);
* }],
* email_link: [&apos;write_file&apos;, function(results, callback) {
* // once the file is written let&apos;s email a link to it...
* callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;user@example.com&apos;});
* }]
* });
* console.log(&apos;results = &apos;, results);
* // results = {
* // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
* // make_folder; &apos;folder&apos;,
* // write_file: &apos;filename&apos;
* // email_link: { file: &apos;filename&apos;, email: &apos;user@example.com&apos; }
* // }
* }
* catch (err) {
* console.log(err);
* }
* }
*
*/
export default function auto(tasks, concurrency, callback) {
if (typeof concurrency !== &apos;number&apos;) {
Expand Down
72 changes: 70 additions & 2 deletions docs/v3/concat.js.html
Expand Up @@ -100,9 +100,77 @@ <h1 class="page-title">concat.js</h1>
* @returns A Promise, if no callback is passed
* @example
*
* async.concat([&apos;dir1&apos;,&apos;dir2&apos;,&apos;dir3&apos;], fs.readdir, function(err, files) {
* // files is now a list of filenames that exist in the 3 directories
* // dir1 is a directory that contains file1.txt, file2.txt
* // dir2 is a directory that contains file3.txt, file4.txt
* // dir3 is a directory that contains file5.txt
* // dir4 does not exist
*
* let directoryList = [&apos;dir1&apos;,&apos;dir2&apos;,&apos;dir3&apos;];
* let withMissingDirectoryList = [&apos;dir1&apos;,&apos;dir2&apos;,&apos;dir3&apos;, &apos;dir4&apos;];
*
* // Using callbacks
* async.concat(directoryList, fs.readdir, function(err, results) {
* if (err) {
* console.log(err);
* } else {
* console.log(results);
* // [ &apos;file1.txt&apos;, &apos;file2.txt&apos;, &apos;file3.txt&apos;, &apos;file4.txt&apos;, file5.txt ]
* }
* });
*
* // Error Handling
* async.concat(withMissingDirectoryList, fs.readdir, function(err, results) {
* if (err) {
* console.log(err);
* // [ Error: ENOENT: no such file or directory ]
* // since dir4 does not exist
* } else {
* console.log(results);
* }
* });
*
* // Using Promises
* async.concat(directoryList, fs.readdir)
* .then(results =&gt; {
* console.log(results);
* // [ &apos;file1.txt&apos;, &apos;file2.txt&apos;, &apos;file3.txt&apos;, &apos;file4.txt&apos;, file5.txt ]
* }).catch(err =&gt; {
* console.log(err);
* });
*
* // Error Handling
* async.concat(withMissingDirectoryList, fs.readdir)
* .then(results =&gt; {
* console.log(results);
* }).catch(err =&gt; {
* console.log(err);
* // [ Error: ENOENT: no such file or directory ]
* // since dir4 does not exist
* });
*
* // Using async/await
* async () =&gt; {
* try {
* let results = await async.concat(directoryList, fs.readdir);
* console.log(results);
* // [ &apos;file1.txt&apos;, &apos;file2.txt&apos;, &apos;file3.txt&apos;, &apos;file4.txt&apos;, file5.txt ]
* } catch (err) {
* console.log(err);
* }
* }
*
* // Error Handling
* async () =&gt; {
* try {
* let results = await async.concat(withMissingDirectoryList, fs.readdir);
* console.log(results);
* } catch (err) {
* console.log(err);
* // [ Error: ENOENT: no such file or directory ]
* // since dir4 does not exist
* }
* }
*
*/
function concat(coll, iteratee, callback) {
return concatLimit(coll, Infinity, iteratee, callback)
Expand Down
45 changes: 40 additions & 5 deletions docs/v3/detect.js.html
Expand Up @@ -107,13 +107,48 @@ <h1 class="page-title">detect.js</h1>
* @returns A Promise, if no callback is passed
* @example
*
* async.detect([&apos;file1&apos;,&apos;file2&apos;,&apos;file3&apos;], function(filePath, callback) {
* fs.access(filePath, function(err) {
* callback(null, !err)
* });
* }, function(err, result) {
* // dir1 is a directory that contains file1.txt, file2.txt
* // dir2 is a directory that contains file3.txt, file4.txt
* // dir3 is a directory that contains file5.txt
*
* // asynchronous function that checks if a file exists
* function fileExists(file, callback) {
* fs.access(file, fs.constants.F_OK, (err) =&gt; {
* callback(null, !err);
* });
* }
*
* async.detect([&apos;file3.txt&apos;,&apos;file2.txt&apos;,&apos;dir1/file1.txt&apos;], fileExists,
* function(err, result) {
* console.log(result);
* // dir1/file1.txt
* // result now equals the first file in the list that exists
* }
*);
*
* // Using Promises
* async.detect([&apos;file3.txt&apos;,&apos;file2.txt&apos;,&apos;dir1/file1.txt&apos;], fileExists)
* .then(result =&gt; {
* console.log(result);
* // dir1/file1.txt
* // result now equals the first file in the list that exists
* }).catch(err =&gt; {
* console.log(err);
* });
*
* // Using async/await
* async () =&gt; {
* try {
* let result = await async.detect([&apos;file3.txt&apos;,&apos;file2.txt&apos;,&apos;dir1/file1.txt&apos;], fileExists);
* console.log(result);
* // dir1/file1.txt
* // result now equals the file in the list that exists
* }
* catch (err) {
* console.log(err);
* }
* }
*
*/
function detect(coll, iteratee, callback) {
return createTester(bool =&gt; bool, (res, item) =&gt; item)(eachOf, coll, iteratee, callback)
Expand Down