-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtp.js
More file actions
87 lines (81 loc) · 2.5 KB
/
tp.js
File metadata and controls
87 lines (81 loc) · 2.5 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
87
// initiate the booking
// add he guests
// process the payment
// 1. Function to create booking
function initBooking(name) {
return new Promise((resolve, reject) => {
if (!name) {
reject("Name is required to create booking");
return;
}
setTimeout(() => {
console.log("Booking initated");
resolve({ // if resolved, then add follwing terms in it
bookingId: "regsf7f6sd7",
name
})
}, 2000);
})
}
// 2. Function to add guest in the created booking
function addTheGuests(booking, guestInfo) {
return new Promise((resolve, reject) => {
if (!guestInfo || guestInfo.length === 0) {
reject("Guest information missing");
return;
}
setTimeout(() => {
console.log("Added guest");
booking.guest = guestInfo; // add guest(s) in booking object
resolve(booking);
}, 2000);
})
}
// 3. Function to add payment details in the created booking
function processPayments(booking, payment) {
return new Promise((resolve, reject) => {
if (!payment || payment.amount <= 0) {
reject("Invalid payment details");
return;
}
setTimeout(() => {
console.log("Payment processed");
booking.payment = payment; // add payment details in booking object
resolve(booking);
}, 2000);
})
}
// // Using .then() and .catch()
// initBooking()
// .then((data) => {
// console.log("Got the booking", data);
// return addTheGuests(data, ["Shikhil", "Rane"])
// })
// .then((booking) => {
// console.log("Added guest to same booking", booking);
// return processPayments(booking, {
// paymentId: "nrjknwtji45rt",
// amount: 3000
// })
// })
// .then((booking) => {
// console.log("Got the booking", booking);
// })
// .catch((err) => {
// console.error("Got the error :", err);
// })
// Using async/await
async function bookingFlow() {
try {
let booking1 = await initBooking();
let booking2 = await addTheGuests(booking1, ["Shikhil", "Rane"]);
let booking3 = await processPayments(booking2, {
paymentId: "nrjknwtji45rt",
amount: 3000
});
console.log("Final booking:", booking3);
} catch (error) {
console.error("Booking failed:", error);
}
}
bookingFlow()