-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasync3.js
86 lines (80 loc) · 1.71 KB
/
async3.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var boldAsync = function(text, callback) {
setTimeout(function (text) {
try {
callback(null, text.bold());
} catch (exception) {
callback(exception);
}
}, 100, text);
};
var capitalizeAsync = function(text, callback) {
setTimeout(function (text) {
try {
callback(null, text.toUpperCase());
} catch (exception) {
callback(exception);
}
}, 100, text);
};
capitalizeAsync(null, function(err, result1) {
if (!err) {
boldAsync(result1, function(err, result2) {
if (!err) {
console.log("Async result is " + result2);
} else {
console.log("Handling async error: " + err);
}
});
} else {
console.log("Handling async error: " + err);
}
});
var handleError = function(err, fn) {
if (err) {
console.log("Handling async error: " + err);
} else {
fn();
}
}
capitalizeAsync(null, function(err, result1) {
handleError(err, function () {
boldAsync(result1, function(err, result2) {
handleError(err, function () {
console.log("Async result is " + result2);
});
});
});
});
var step = require("step");
step(
function () {
capitalizeAsync("testtext", this);
},
function (err, result) {
if (err) throw err;
boldAsync(result, this);
},
function(err, result) {
if (err) {
console.log("Handling async error: " + err);
} else {
console.log("Async result is " + result);
}
}
);
step(
function () {
capitalizeAsync(null, this);
},
function (err, result) {
if (err) throw err;
boldAsync(result, this);
},
function(err, result) {
if (err) {
console.log("Handling async error: " + err);
} else {
console.log("Async result is " + result);
}
}
);