-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
89255fe
commit 159a119
Showing
40 changed files
with
5,267 additions
and
637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, 'data.txt', 'utf-8'), | ||
* showData: ['readData', function(results, cb) { | ||
* // results.readData is the file's contents | ||
* // ... | ||
* get_data: function(callback) { | ||
* // async code to get some data | ||
* callback(null, 'data', 'converted to array'); | ||
* }, | ||
* 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, 'folder'); | ||
* }, | ||
* write_file: ['get_data', 'make_folder', function(results, callback) { | ||
* // 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) { | ||
* // once the file is written let's email a link to it... | ||
* callback(null, {'file':results.write_file, 'email':'[email protected]'}); | ||
* }] | ||
* }, callback); | ||
* }, function(err, results) { | ||
* if (err) { | ||
* console.log('err = ', err); | ||
* } | ||
* console.log('results = ', results); | ||
* // results = { | ||
* // get_data: ['data', 'converted to array'] | ||
* // make_folder; 'folder', | ||
* // write_file: 'filename' | ||
* // email_link: { file: 'filename', email: '[email protected]' } | ||
* // } | ||
* }); | ||
* | ||
* //Using Promises | ||
* async.auto({ | ||
* get_data: function(callback) { | ||
* console.log('in get_data'); | ||
|
@@ -143,21 +168,62 @@ <h1 class="page-title">auto.js</h1> | |
* 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':'[email protected]'}); | ||
* }] | ||
* }, function(err, results) { | ||
* console.log('err = ', err); | ||
* }).then(results => { | ||
* console.log('results = ', results); | ||
* // results = { | ||
* // get_data: ['data', 'converted to array'] | ||
* // make_folder; 'folder', | ||
* // write_file: 'filename' | ||
* // email_link: { file: 'filename', email: '[email protected]' } | ||
* // } | ||
* }).catch(err => { | ||
* console.log('err = ', err); | ||
* }); | ||
* | ||
* //Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.auto({ | ||
* get_data: function(callback) { | ||
* // async code to get some data | ||
* callback(null, 'data', 'converted to array'); | ||
* }, | ||
* 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, 'folder'); | ||
* }, | ||
* write_file: ['get_data', 'make_folder', function(results, callback) { | ||
* // 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) { | ||
* // once the file is written let's email a link to it... | ||
* callback(null, {'file':results.write_file, 'email':'[email protected]'}); | ||
* }] | ||
* }); | ||
* console.log('results = ', results); | ||
* // results = { | ||
* // get_data: ['data', 'converted to array'] | ||
* // make_folder; 'folder', | ||
* // write_file: 'filename' | ||
* // email_link: { file: 'filename', email: '[email protected]' } | ||
* // } | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
export default function auto(tasks, concurrency, callback) { | ||
if (typeof concurrency !== 'number') { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.