Working on an Angular project involves developing a booking system with various scenarios. The challenge lies in handling different server calls based on the response of a promise, leading to a nested callback structure that contradicts the purpose of promises. Functions are being called without returning anything but rather initiating another function.
Outlined below is a simplified decision tree:
Create a booking
1.1 (success) - Retrieve booking details
- 1.1.1 (success) - Display booking details to the user
- 1.1.2 (fail) - Present retry option
1.2 (fail) - Check errorCode
- 1.2.1 if (errorCode === 'soldout')
- 1.2.1.1 - Attempt alternative booking
- 1.2.1.1.1 (success) Load booking details
- 1.2.1.1.1.1 (success) CheckPriceDifference()
- 1.2.1.1.1.2 (fail) Show soldOutMessage();
- 1.2.1 if (errorCode === 'soldout')
1.2.2 if (errorCode != 'soldout') * Display retry button.
The code implementation reflects this logic:
makeBooking()
.then(function(response){
successHandler(response);
}, function(error){
errorHandler(response);
})
successHandler(response){
loadDetails(response)
.then(function(details){
showDetails(details);
}, function(error){
handleDetailsError(error);
}
}
errorHandler(response){
if ( checkSoldout(reponse) ){
makeAlternativeBooking();
}
}
Does this approach effectively manage the complexity of working with nested promises?