The issue at hand:
I am faced with the task of checking multiple conditions, some of which lead to the same outcome.
Here is the current flow:
- First, I check if a form is saved locally
- If it is saved locally, I display text 1 to the user
- If not saved locally, I then check if the server has this form in its database
- If the server does have the form, I show text 2
- If the server does not have the form, I show text 1
Is there a more efficient way to handle these conditions that ultimately lead to the same result?
Currently, the logic looks like this:
iif(
() => this.savedLocally,
this.showText1(),
this.checkOnServer().pipe(
switchMap(onServer => iif(
() => onServer,
this.showText2(),
this.showText1()
))
)
)
If another condition is added, it adds another level repeating this.showText1()
somewhere.
I am seeking a redesign of this logic to minimize repetition.
The challenge lies in the fact that all three checks need to be performed in a specific order, and they are all asynchronous.
If it were a simple if
, I could use
if (cond1 && cond2 && cond3)
, but the situation here is more complex. The checks must be sequential and each one depends on the result of the previous check.