Skip to content

Instantly share code, notes, and snippets.

@vgrichina
Last active November 28, 2018 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrichina/d7e984b986aafeb5192aded4befdb5b4 to your computer and use it in GitHub Desktop.
Save vgrichina/d7e984b986aafeb5192aded4befdb5b4 to your computer and use it in GitHub Desktop.
Booking train and hotel
book_train_and_hotel() : void { // Needs 6 MANA.
// 3 for train and callback and 3 for hotel and callback.
// E.g. book_train and book_hotel doesn't need additional mana, so only 1 MANA for a call.
// Callback may need 1 additional to cancel train or hotel booking if one of them failed.
assert_has_mana(6);
let hotelBooking = bookHotel.withMana(3).withCoins(1000).book(sender());
let trainBooking = bookTrain.withMana(3).withCoins(1000).book(sender());
Promise.all(hotelBooking, trainBooking)
.withMana(7)
.catch(_bookingFailed.bind(sender(), /* whatever info about "session" needed */));
.withMana(1)
.then(_bookingCompleted.bind(sender(), /* whatever info about "session" needed */));
}
_bookingCompleted(client, /* session args */, hotel: HotelBookingResult, train: TrainBookingResult) {
// This sends event to original caller
client.onBookingCompleted(hotel, train);
}
_bookingFailed(client, /* session args */, errors: Array<Error>) {
bookHotel.withMana(3).cancel(client);
bookTrain.withMana(3).cancel(client);
client.onBookingFailed(...);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment