diff options
author | Roman Lorenzo Balayan <roman.balayan@gmail.com> | 2021-08-06 06:58:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-05 15:58:10 -0700 |
commit | 159a119fbb1a585c61f33b4b3b5036f5d332ebbb (patch) | |
tree | bbf78b2e8899c412ceede4a705d45368f61b51bf /docs/v3/auto.js.html | |
parent | 89255fe326050e80ce5394a9c00e11e9be8b1005 (diff) | |
download | async-159a119fbb1a585c61f33b4b3b5036f5d332ebbb.tar.gz |
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 <roman.balayan@paymaya.com>
Diffstat (limited to 'docs/v3/auto.js.html')
-rw-r--r-- | docs/v3/auto.js.html | 88 |
1 files changed, 77 insertions, 11 deletions
diff --git a/docs/v3/auto.js.html b/docs/v3/auto.js.html index e380d65..a1d65db 100644 --- a/docs/v3/auto.js.html +++ b/docs/v3/auto.js.html @@ -121,15 +121,40 @@ import { promiseCallback, PROMISE_SYMBOL } from './internal/promiseCallback * @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':'user@example.com'}); * }] - * }, 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: 'user@example.com' } + * // } + * }); * + * //Using Promises * async.auto({ * get_data: function(callback) { * console.log('in get_data'); @@ -143,21 +168,62 @@ import { promiseCallback, PROMISE_SYMBOL } from './internal/promiseCallback * 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); + * }).then(results => { * console.log('results = ', results); + * // results = { + * // get_data: ['data', 'converted to array'] + * // make_folder; 'folder', + * // write_file: 'filename' + * // email_link: { file: 'filename', email: 'user@example.com' } + * // } + * }).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':'user@example.com'}); + * }] + * }); + * console.log('results = ', results); + * // results = { + * // get_data: ['data', 'converted to array'] + * // make_folder; 'folder', + * // write_file: 'filename' + * // email_link: { file: 'filename', email: 'user@example.com' } + * // } + * } + * catch (err) { + * console.log(err); + * } + * } + * */ export default function auto(tasks, concurrency, callback) { if (typeof concurrency !== 'number') { |