diff options
Diffstat (limited to 'docs/v3/module-ControlFlow.html')
-rw-r--r-- | docs/v3/module-ControlFlow.html | 316 |
1 files changed, 283 insertions, 33 deletions
diff --git a/docs/v3/module-ControlFlow.html b/docs/v3/module-ControlFlow.html index 0b41c93..d48a8a7 100644 --- a/docs/v3/module-ControlFlow.html +++ b/docs/v3/module-ControlFlow.html @@ -853,15 +853,40 @@ will only contain partial results. Invoked with (err, results).</p></td> <h5>Example</h5> - <pre class="prettyprint"><code>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 - // ... + <pre class="prettyprint"><code>//Using Callbacks +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'}); }] -}, 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'); @@ -875,21 +900,61 @@ async.auto({ 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); -});</code></pre> + // 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); + } +}</code></pre> @@ -2692,7 +2757,8 @@ Invoked with (err, results).</p></td> <h5>Example</h5> - <pre class="prettyprint"><code>async.parallel([ + <pre class="prettyprint"><code>//Using Callbacks +async.parallel([ function(callback) { setTimeout(function() { callback(null, 'one'); @@ -2703,10 +2769,9 @@ Invoked with (err, results).</p></td> callback(null, 'two'); }, 100); } -], -// optional callback -function(err, results) { - // the results array will equal ['one','two'] even though +], function(err, results) { + console.log(results); + // results is equal to ['one','two'] even though // the second function had a shorter timeout. }); @@ -2723,8 +2788,95 @@ async.parallel({ }, 100); } }, function(err, results) { - // results is now equals to: {one: 1, two: 2} -});</code></pre> + console.log(results); + // results is equal to: { one: 1, two: 2 } +}); + +//Using Promises +async.parallel([ + function(callback) { + setTimeout(function() { + callback(null, 'one'); + }, 200); + }, + function(callback) { + setTimeout(function() { + callback(null, 'two'); + }, 100); + } +]).then(results => { + console.log(results); + // results is equal to ['one','two'] even though + // the second function had a shorter timeout. +}).catch(err => { + console.log(err); +}); + +// an example using an object instead of an array +async.parallel({ + one: function(callback) { + setTimeout(function() { + callback(null, 1); + }, 200); + }, + two: function(callback) { + setTimeout(function() { + callback(null, 2); + }, 100); + } +}).then(results => { + console.log(results); + // results is equal to: { one: 1, two: 2 } +}).catch(err => { + console.log(err); +}); + +//Using async/await +async () => { + try { + let results = await async.parallel([ + function(callback) { + setTimeout(function() { + callback(null, 'one'); + }, 200); + }, + function(callback) { + setTimeout(function() { + callback(null, 'two'); + }, 100); + } + ]); + console.log(results); + // results is equal to ['one','two'] even though + // the second function had a shorter timeout. + } + catch (err) { + console.log(err); + } +} + +// an example using an object instead of an array +async () => { + try { + let results = await async.parallel({ + one: function(callback) { + setTimeout(function() { + callback(null, 1); + }, 200); + }, + two: function(callback) { + setTimeout(function() { + callback(null, 2); + }, 100); + } + }); + console.log(results); + // results is equal to: { one: 1, two: 2 } + } + catch (err) { + console.log(err); + } +}</code></pre> @@ -4513,35 +4665,133 @@ with (err, result).</p></td> <h5>Example</h5> - <pre class="prettyprint"><code>async.series([ + <pre class="prettyprint"><code>//Using Callbacks +async.series([ function(callback) { - // do some stuff ... - callback(null, 'one'); + setTimeout(function() { + // do some async task + callback(null, 'one'); + }, 200); }, function(callback) { - // do some more stuff ... - callback(null, 'two'); + setTimeout(function() { + // then do another async task + callback(null, 'two'); + }, 100); } -], -// optional callback -function(err, results) { - // results is now equal to ['one', 'two'] +], function(err, results) { + console.log(results); + // results is equal to ['one','two'] }); +// an example using objects instead of arrays async.series({ one: function(callback) { setTimeout(function() { + // do some async task callback(null, 1); }, 200); }, - two: function(callback){ + two: function(callback) { setTimeout(function() { + // then do another async task callback(null, 2); }, 100); } }, function(err, results) { - // results is now equal to: {one: 1, two: 2} -});</code></pre> + console.log(results); + // results is equal to: { one: 1, two: 2 } +}); + +//Using Promises +async.series([ + function(callback) { + setTimeout(function() { + callback(null, 'one'); + }, 200); + }, + function(callback) { + setTimeout(function() { + callback(null, 'two'); + }, 100); + } +]).then(results => { + console.log(results); + // results is equal to ['one','two'] +}).catch(err => { + console.log(err); +}); + +// an example using an object instead of an array +async.series({ + one: function(callback) { + setTimeout(function() { + // do some async task + callback(null, 1); + }, 200); + }, + two: function(callback) { + setTimeout(function() { + // then do another async task + callback(null, 2); + }, 100); + } +}).then(results => { + console.log(results); + // results is equal to: { one: 1, two: 2 } +}).catch(err => { + console.log(err); +}); + +//Using async/await +async () => { + try { + let results = await async.series([ + function(callback) { + setTimeout(function() { + // do some async task + callback(null, 'one'); + }, 200); + }, + function(callback) { + setTimeout(function() { + // then do another async task + callback(null, 'two'); + }, 100); + } + ]); + console.log(results); + // results is equal to ['one','two'] + } + catch (err) { + console.log(err); + } +} + +// an example using an object instead of an array +async () => { + try { + let results = await async.parallel({ + one: function(callback) { + setTimeout(function() { + // do some async task + callback(null, 1); + }, 200); + }, + two: function(callback) { + setTimeout(function() { + // then do another async task + callback(null, 2); + }, 100); + } + }); + console.log(results); + // results is equal to: { one: 1, two: 2 } + } + catch (err) { + console.log(err); + } +}</code></pre> @@ -5652,7 +5902,7 @@ callback. Invoked with (err, [results]);</p></td> <pre class="prettyprint"><code>const results = [] let finished = false -async.until(function test(page, cb) { +async.until(function test(cb) { cb(null, finished) }, function iter(next) { fetchPage(url, (err, body) => { |