@@ -121,15 +121,40 @@ <h1 class="page-title">auto.js</h1>
121
121
* @returns {Promise} a promise, if a callback is not passed
122
122
* @example
123
123
*
124
+ * //Using Callbacks
124
125
* 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] '});
130
143
* }]
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
+ * });
132
156
*
157
+ * //Using Promises
133
158
* async.auto({
134
159
* get_data: function(callback) {
135
160
* console.log('in get_data');
@@ -143,21 +168,62 @@ <h1 class="page-title">auto.js</h1>
143
168
* callback(null, 'folder');
144
169
* },
145
170
* write_file: ['get_data', 'make_folder', function(results, callback) {
146
- * console.log('in write_file', JSON.stringify(results));
147
171
* // once there is some data and the directory exists,
148
172
* // write the data to a file in the directory
149
173
* callback(null, 'filename');
150
174
* }],
151
175
* email_link: ['write_file', function(results, callback) {
152
- * console.log('in email_link', JSON.stringify(results));
153
176
* // once the file is written let's email a link to it...
154
- * // results.write_file contains the filename returned by write_file.
155
177
* callback(null, {'file':results.write_file, 'email':'
[email protected] '});
156
178
* }]
157
- * }, function(err, results) {
158
- * console.log('err = ', err);
179
+ * }).then(results => {
159
180
* 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);
160
189
* });
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
+ *
161
227
*/
162
228
export default function auto(tasks, concurrency, callback) {
163
229
if (typeof concurrency !== 'number') {
0 commit comments