This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumIsEvenAsync() = async { | |
val r1 = async { myRandomNumberSuccess(OPERATION_IS_EVEN) } | |
val r2 = async { myRandomNumberSuccess(OPERATION_IS_EVEN) } | |
val r3 = async { myRandomNumberSuccess(OPERATION_IS_EVEN) } | |
val sum = r1.await() + r2.await() + r3.await() | |
log("$OPERATION_IS_EVEN: Sum = $sum") | |
sum % 2 == 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runBlocking { | |
val defIsEven = operator.sumIsEvenAsync() | |
launch { | |
try { | |
log("${Operator.OPERATION_IS_EVEN} Result = ${defIsEven.await()}") | |
} catch (e: Exception) { | |
log("${Operator.OPERATION_IS_EVEN} Caught: [ $e ]") | |
} finally { | |
log("--- NOTHING CONTAINING \"${Operator.OPERATION_IS_EVEN}\" SHOULD APPEAR AFTER THIS LINE ---") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumAverageAsync() = async { | |
val r1 = async { myRandomNumberSuccess(OPERATION_AVERAGE) } | |
val r2 = async { myRandomNumberSuccess(OPERATION_AVERAGE) } | |
val r3 = async { myRandomNumberSuccess(OPERATION_AVERAGE) } | |
val sum = r1.await() + r2.await() + r3.await() | |
log("$OPERATION_AVERAGE: Sum = $sum") | |
sum.toFloat() / 3 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runBlocking { | |
val defIsEven = operator.sumIsEvenAsync() | |
val defAverage = operator.sumAverageAsync() | |
launch { | |
try { | |
log("Operation [Is Even] Result = ${defIsEven.await()}") | |
} catch (e: Exception) { | |
log("Operation [Is Even] Caught: [ $e ]") | |
} finally { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumSquareAsync() = async { | |
val r1 = async { myRandomNumberSuccess(OPERATION_SQUARE) } | |
val r2 = async { myRandomNumberSuccess(OPERATION_SQUARE) } | |
val r3 = async { myRandomNumberSuccess(OPERATION_SQUARE) } | |
val sum = r1.await() + r2.await() + r3.await() | |
log("$OPERATION_SQUARE: Sum = $sum") | |
sum * sum | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runBlocking { | |
val defIsEven = operator.sumIsEvenAsync() | |
val defAverage = operator.sumAverageAsync() | |
val defSquare = operator.sumSquareAsync() | |
launch { | |
try { | |
log("${Operator.OPERATION_IS_EVEN} Result = ${defIsEven.await()}") | |
} catch (e: Exception) { | |
log("${Operator.OPERATION_IS_EVEN} Caught: [ $e ]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumIsEvenAsync() = async { | |
val sum = sumThreeRandomNumbers(OPERATION_IS_EVEN) | |
log("$OPERATION_IS_EVEN: Sum = $sum") | |
sum % 2 == 0 | |
} | |
fun sumAverageAsync() = async { | |
val sum = sumThreeRandomNumbers(OPERATION_AVERAGE) | |
log("$OPERATION_AVERAGE: Sum = $sum") | |
sum.toFloat() / 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private suspend fun sumThreeRandomNumbers(operation: String): Int { | |
val sum = withContext(Dispatchers.Default) { | |
val r1 = async { myRandomNumberFail(operation) } | |
val r2 = async { myRandomNumberSuccess(operation) } | |
val r3 = async { myRandomNumberSuccess(operation) } | |
r1.await() + r2.await() + r3.await() | |
} | |
return sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private suspend fun sumThreeRandomNumbers(operation: String): Int { | |
val sum = coroutineScope { | |
val r1 = async { myRandomNumberFail(operation) } | |
val r2 = async { myRandomNumberSuccess(operation) } | |
val r3 = async { myRandomNumberSuccess(operation) } | |
r1.await() + r2.await() + r3.await() | |
} | |
return sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private suspend fun sumThreeRandomNumbers(operation: String): Int = coroutineScope { | |
val r1 = async { myRandomNumberFail(operation) } | |
val r2 = async { myRandomNumberSuccess(operation) } | |
val r3 = async { myRandomNumberSuccess(operation) } | |
r1.await() + r2.await() + r3.await() | |
} |