When I only perform the next steps in my algorithm if various conditions are met I express it this way:
if (sc1 || sc2) {
do();
various();
things();
}
When I only perform the next steps based on the fulfillment of a promise I can express it like this:
asyncCheck().then(ac1 => {
if (ac1) {
do();
various();
things();
}
}
How can I express in idiomatic JavaScript when condition sc1
is a just a regular old synchronous expression but condition ac2
comes asynchronously via a promise?
Assume native ES6 promises and nontrivial code that gets executed if the conditions are met.
For instance, this "obvious" way seems ugly:
if (sc1) {
do();
various();
things();
} else {
asyncCheck().then(ac2 => {
if (ac2) {
do();
various();
things();
}
}
}
I could put the repeated code in a function that gets called either way, which is less ugly, but I feel like I could be missing something more idiomatic that other JavaScript programmers might be using.
I should add this observation too: Since in my case, there is a logical or, it should short circuit so it shouldn't bother with the slow deferred check if the simple check is already false
.