summaryrefslogtreecommitdiff
path: root/docs/v3/module-ControlFlow.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/v3/module-ControlFlow.html')
-rw-r--r--docs/v3/module-ControlFlow.html316
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, &apos;data.txt&apos;, &apos;utf-8&apos;),
- showData: [&apos;readData&apos;, function(results, cb) {
- // results.readData is the file&apos;s contents
- // ...
+ <pre class="prettyprint"><code>//Using Callbacks
+async.auto({
+ get_data: function(callback) {
+ // async code to get some data
+ callback(null, &apos;data&apos;, &apos;converted to array&apos;);
+ },
+ 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, &apos;folder&apos;);
+ },
+ write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
+ // once there is some data and the directory exists,
+ // write the data to a file in the directory
+ callback(null, &apos;filename&apos;);
+ }],
+ email_link: [&apos;write_file&apos;, function(results, callback) {
+ // once the file is written let&apos;s email a link to it...
+ callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;user@example.com&apos;});
}]
-}, callback);
+}, function(err, results) {
+ if (err) {
+ console.log(&apos;err = &apos;, err);
+ }
+ console.log(&apos;results = &apos;, results);
+ // results = {
+ // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
+ // make_folder; &apos;folder&apos;,
+ // write_file: &apos;filename&apos;
+ // email_link: { file: &apos;filename&apos;, email: &apos;user@example.com&apos; }
+ // }
+});
+//Using Promises
async.auto({
get_data: function(callback) {
console.log(&apos;in get_data&apos;);
@@ -875,21 +900,61 @@ async.auto({
callback(null, &apos;folder&apos;);
},
write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
- console.log(&apos;in write_file&apos;, JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, &apos;filename&apos;);
}],
email_link: [&apos;write_file&apos;, function(results, callback) {
- console.log(&apos;in email_link&apos;, JSON.stringify(results));
// once the file is written let&apos;s email a link to it...
- // results.write_file contains the filename returned by write_file.
callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;user@example.com&apos;});
}]
-}, function(err, results) {
- console.log(&apos;err = &apos;, err);
+}).then(results =&gt; {
console.log(&apos;results = &apos;, results);
-});</code></pre>
+ // results = {
+ // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
+ // make_folder; &apos;folder&apos;,
+ // write_file: &apos;filename&apos;
+ // email_link: { file: &apos;filename&apos;, email: &apos;user@example.com&apos; }
+ // }
+}).catch(err =&gt; {
+ console.log(&apos;err = &apos;, err);
+});
+
+//Using async/await
+async () =&gt; {
+ try {
+ let results = await async.auto({
+ get_data: function(callback) {
+ // async code to get some data
+ callback(null, &apos;data&apos;, &apos;converted to array&apos;);
+ },
+ 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, &apos;folder&apos;);
+ },
+ write_file: [&apos;get_data&apos;, &apos;make_folder&apos;, function(results, callback) {
+ // once there is some data and the directory exists,
+ // write the data to a file in the directory
+ callback(null, &apos;filename&apos;);
+ }],
+ email_link: [&apos;write_file&apos;, function(results, callback) {
+ // once the file is written let&apos;s email a link to it...
+ callback(null, {&apos;file&apos;:results.write_file, &apos;email&apos;:&apos;user@example.com&apos;});
+ }]
+ });
+ console.log(&apos;results = &apos;, results);
+ // results = {
+ // get_data: [&apos;data&apos;, &apos;converted to array&apos;]
+ // make_folder; &apos;folder&apos;,
+ // write_file: &apos;filename&apos;
+ // email_link: { file: &apos;filename&apos;, email: &apos;user@example.com&apos; }
+ // }
+ }
+ 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, &apos;one&apos;);
@@ -2703,10 +2769,9 @@ Invoked with (err, results).</p></td>
callback(null, &apos;two&apos;);
}, 100);
}
-],
-// optional callback
-function(err, results) {
- // the results array will equal [&apos;one&apos;,&apos;two&apos;] even though
+], function(err, results) {
+ console.log(results);
+ // results is equal to [&apos;one&apos;,&apos;two&apos;] 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, &apos;one&apos;);
+ }, 200);
+ },
+ function(callback) {
+ setTimeout(function() {
+ callback(null, &apos;two&apos;);
+ }, 100);
+ }
+]).then(results =&gt; {
+ console.log(results);
+ // results is equal to [&apos;one&apos;,&apos;two&apos;] even though
+ // the second function had a shorter timeout.
+}).catch(err =&gt; {
+ 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 =&gt; {
+ console.log(results);
+ // results is equal to: { one: 1, two: 2 }
+}).catch(err =&gt; {
+ console.log(err);
+});
+
+//Using async/await
+async () =&gt; {
+ try {
+ let results = await async.parallel([
+ function(callback) {
+ setTimeout(function() {
+ callback(null, &apos;one&apos;);
+ }, 200);
+ },
+ function(callback) {
+ setTimeout(function() {
+ callback(null, &apos;two&apos;);
+ }, 100);
+ }
+ ]);
+ console.log(results);
+ // results is equal to [&apos;one&apos;,&apos;two&apos;] even though
+ // the second function had a shorter timeout.
+ }
+ catch (err) {
+ console.log(err);
+ }
+}
+
+// an example using an object instead of an array
+async () =&gt; {
+ 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, &apos;one&apos;);
+ setTimeout(function() {
+ // do some async task
+ callback(null, &apos;one&apos;);
+ }, 200);
},
function(callback) {
- // do some more stuff ...
- callback(null, &apos;two&apos;);
+ setTimeout(function() {
+ // then do another async task
+ callback(null, &apos;two&apos;);
+ }, 100);
}
-],
-// optional callback
-function(err, results) {
- // results is now equal to [&apos;one&apos;, &apos;two&apos;]
+], function(err, results) {
+ console.log(results);
+ // results is equal to [&apos;one&apos;,&apos;two&apos;]
});
+// 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, &apos;one&apos;);
+ }, 200);
+ },
+ function(callback) {
+ setTimeout(function() {
+ callback(null, &apos;two&apos;);
+ }, 100);
+ }
+]).then(results =&gt; {
+ console.log(results);
+ // results is equal to [&apos;one&apos;,&apos;two&apos;]
+}).catch(err =&gt; {
+ 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 =&gt; {
+ console.log(results);
+ // results is equal to: { one: 1, two: 2 }
+}).catch(err =&gt; {
+ console.log(err);
+});
+
+//Using async/await
+async () =&gt; {
+ try {
+ let results = await async.series([
+ function(callback) {
+ setTimeout(function() {
+ // do some async task
+ callback(null, &apos;one&apos;);
+ }, 200);
+ },
+ function(callback) {
+ setTimeout(function() {
+ // then do another async task
+ callback(null, &apos;two&apos;);
+ }, 100);
+ }
+ ]);
+ console.log(results);
+ // results is equal to [&apos;one&apos;,&apos;two&apos;]
+ }
+ catch (err) {
+ console.log(err);
+ }
+}
+
+// an example using an object instead of an array
+async () =&gt; {
+ 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) =&gt; {