-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_nonblocking_loop.js
More file actions
52 lines (46 loc) · 1.33 KB
/
03_nonblocking_loop.js
File metadata and controls
52 lines (46 loc) · 1.33 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
48
49
50
51
52
// An example of how to work with an array in a non-blocking
// loop.
// This will store our results.
var result = {
count: 0,
sum: 0,
toString: function () {
return "Count: " + result.count + ", Sum: " + result.sum;
}
};
// Let's actually declare our addtion function here.
var addition = function (array, callback) {
// We're using setImmediate to give control back to the Node process.
// We will run once, then queue up the next callback.
var additionAsync = function (value, callback) {
result.count += 1;
result.sum += value;
setImmediate(function () {
callback();
});
};
// I don't think there's anything here that could throw an error,
// but we don't actually want to throw the error regardless.
// Instead, lets catch it and include it in the callback.
try {
if (array.length > 0) {
// #shift() pops the first item off the array.
additionAsync(array.shift(), function () {
addition(array, callback);
});
} else {
// There are no more items in the array, so fire the callback.
callback(null, result);
}
} catch (e) {
callback(e, result);
}
};
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
addition(array, function(error, data) {
if (error) {
console.error("Error: " + error);
return;
}
console.log("Result: " + data);
});