-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtryCatch.js
52 lines (49 loc) · 1.51 KB
/
tryCatch.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
// Process a command
function processCommand(command, callback) {
if (command.operation === 'multiply') {
var result = command.a * command.b;
callback(null, result);
} else {
callback('Invalid operation');
}
}
// Callback to handle the results of an async function
function resultCallback(err, result) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Result: ' + result);
}
}
// process the function in a synchronous manner.
function processSync(command) {
processCommand(command, resultCallback);
}
// process the function in an async manner.
function processAsync(command) {
// do the calculation on the next tick to make it asynchronous
process.nextTick(
function() {
processCommand(command, resultCallback);
}
);
}
// ############## Run some tests ###################
// now try some different values
// should return 4
processAsync({operation: 'multiply', a: 2, b: 2});
// should return NaN (but not an error) as no operaters defined
processAsync({operation: 'multiply'});
// should throw an error and be caught by the try/catch block as it's synchronous
try {
processSync();
} catch (error) {
console.log('An Error has occurred');
}
// should throw an error which can't be caught by the try/catch block as it's executing asynchronously
try {
processAsync();
console.log("I'D PREFER IF THIS CODE DIDN'T EXECUTE AS THERE'S AN ERROR");
} catch (error) {
console.log('An Error has occurred');
}