-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbackVsEvents.js
More file actions
86 lines (74 loc) · 2.69 KB
/
callbackVsEvents.js
File metadata and controls
86 lines (74 loc) · 2.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
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
// wrong code
function bookCab(user, location, callback) {
validateUser(user, function(err, isValid) {
if (err) {
callback(err);
} else {
findAvailableDriver(location, function(err, driver) {
if (err) {
callback(err);
} else {
reserveCab(driver, function(err, booking) {
if (err) {
callback(err);
} else {
callback(null, booking);
}
});
}
});
}
});
}
// Right Approach
const EventEmitter = require('events');
class CabBooking extends EventEmitter {
bookCab(user, location) {
this.validateUser(user)
.then((isValid) => this.findAvailableDriver(location))
.then((driver) => this.reserveCab(driver))
.then((booking) => this.emit('bookingSuccess', booking))
.catch((error) => this.emit('bookingError', error));
}
validateUser(user) {
return new Promise((resolve, reject) => {
// User validation logic
if (user.isValid()) {
resolve(true);
} else {
reject(new Error('Invalid user'));
}
});
}
findAvailableDriver(location) {
return new Promise((resolve, reject) => {
// Driver finding logic
if (location.hasAvailableDriver()) {
resolve(location.getDriver());
} else {
reject(new Error('No available driver'));
}
});
}
reserveCab(driver) {
return new Promise((resolve) => {
// Cab reservation logic
const booking = createBooking(driver);
resolve(booking);
});
}
}
// Usage
const cabBooking = new CabBooking();
cabBooking.on('bookingSuccess', (booking) => {
console.log('Booking successful:', booking);
});
cabBooking.on('bookingError', (error) => {
console.error('Booking failed:', error);
});
const user = getUser(); // Retrieve user details
const location = getLocation(); // Retrieve location details
cabBooking.bookCab(user, location);
// In the right code approach, events are used to handle asynchronous operations. The code is structured in a more modular and readable way, eliminating the callback nesting.
// The CabBooking class extends EventEmitter and emits events for booking success and failure. Promises are used for handling asynchronous operations, making the code more straightforward and easier to understand. Listeners are attached to the bookingSuccess and bookingError events to handle the corresponding outcomes.
// This approach improves code organization, readability, and maintainability. It avoids callback hell and promotes a more modular and event-driven architecture, making it easier to add, modify, or remove functionality in the cab booking app.