-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncTest.js
More file actions
47 lines (45 loc) · 1.69 KB
/
asyncTest.js
File metadata and controls
47 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var async = require('async')
var data = [];
for (var i = 0; i < 100; i = i + 5) {
data.push(i);
}
console.log('Our Inital Array<br/>');
console.log('====================<br/>');
for (var i = 0; i < 100; i = i + 5) {
console.log(i + "<br/>");
}
console.log('====================' + '<br/>');
console.log('Lets start Parallel Processing' + '<br/>');
async.each(data, processData, function (err) {
if (err) {
console.log('OOPS! How is this possible?' + "<br/>");
}
console.log('Parallel Processing Done<br/>');
console.log('====================' + '<br/>');
console.log('Lets Start Same Operation In Series');
async.eachSeries(data, processData, function (err) {
if (err) {
console.log('OOPS! How is this possible?' + "<br/>");
}
console.log('Series Processing Done<br/>');
console.log('====================' + '<br/>');
console.log('Lets Start Same Operation In Series But With 5 at time');
async.eachLimit(data, 5, processData, function (err) {
if (err) {
console.log('OOPS! How is this possible?' + "<br/>");
}
console.log('Series Processing Done<br/>');
console.log('====================' + '<br/>');
});
});
});
function processData(item, callback) {
//we are just diving the number by 5
//but we can actually do a very long process here
setTimeout(function () {
var date = new Date();
console.log((item / 5) + ' Finished At ' + date.getHours() + ":" + +date.getMinutes() + ":" + +date.getSeconds() + '<br/>');
callback(null); //no error
//incase of error pass the error in callback function
}, 1000);
}