@@ -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, 'data.txt', 'utf-8'),
127- * showData: ['readData', function(results, cb) {
128- * // results.readData is the file's contents
129- * // ...
126+ * get_data: function(callback) {
127+ * // async code to get some data
128+ * callback(null, 'data', 'converted to array');
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, 'folder');
134+ * },
135+ * write_file: ['get_data', 'make_folder', 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, 'filename');
139+ * }],
140+ * email_link: ['write_file', function(results, callback) {
141+ * // once the file is written let's email a link to it...
142+ * callback(null, {'file':results.write_file, 'email':'
[email protected] '});
130143 * }]
131- * }, callback);
144+ * }, function(err, results) {
145+ * if (err) {
146+ * console.log('err = ', err);
147+ * }
148+ * console.log('results = ', results);
149+ * // results = {
150+ * // get_data: ['data', 'converted to array']
151+ * // make_folder; 'folder',
152+ * // write_file: 'filename'
153+ * // email_link: { file: 'filename', email: '
[email protected] ' }
154+ * // }
155+ * });
132156 *
157+ * //Using Promises
133158 * async.auto({
134159 * get_data: function(callback) {
135160 * console.log('in get_data');
@@ -143,21 +168,62 @@ <h1 class="page-title">auto.js</h1>
143168 * callback(null, 'folder');
144169 * },
145170 * write_file: ['get_data', 'make_folder', function(results, callback) {
146- * console.log('in write_file', 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, 'filename');
150174 * }],
151175 * email_link: ['write_file', function(results, callback) {
152- * console.log('in email_link', JSON.stringify(results));
153176 * // once the file is written let's email a link to it...
154- * // results.write_file contains the filename returned by write_file.
155177 * callback(null, {'file':results.write_file, 'email':'
[email protected] '});
156178 * }]
157- * }, function(err, results) {
158- * console.log('err = ', err);
179+ * }).then(results => {
159180 * console.log('results = ', results);
181+ * // results = {
182+ * // get_data: ['data', 'converted to array']
183+ * // make_folder; 'folder',
184+ * // write_file: 'filename'
185+ * // email_link: { file: 'filename', email: '
[email protected] ' }
186+ * // }
187+ * }).catch(err => {
188+ * console.log('err = ', err);
160189 * });
190+ *
191+ * //Using async/await
192+ * async () => {
193+ * try {
194+ * let results = await async.auto({
195+ * get_data: function(callback) {
196+ * // async code to get some data
197+ * callback(null, 'data', 'converted to array');
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, 'folder');
203+ * },
204+ * write_file: ['get_data', 'make_folder', 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, 'filename');
208+ * }],
209+ * email_link: ['write_file', function(results, callback) {
210+ * // once the file is written let's email a link to it...
211+ * callback(null, {'file':results.write_file, 'email':'
[email protected] '});
212+ * }]
213+ * });
214+ * console.log('results = ', results);
215+ * // results = {
216+ * // get_data: ['data', 'converted to array']
217+ * // make_folder; 'folder',
218+ * // write_file: 'filename'
219+ * // email_link: { file: 'filename', email: '
[email protected] ' }
220+ * // }
221+ * }
222+ * catch (err) {
223+ * console.log(err);
224+ * }
225+ * }
226+ *
161227 */
162228export default function auto(tasks, concurrency, callback) {
163229 if (typeof concurrency !== 'number') {
0 commit comments