Created
September 27, 2018 09:26
-
-
Save elizarov/d16664152fa65f6bd794319a5b1ed9ab to your computer and use it in GitHub Desktop.
DeferredCache
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 <T : Any> CoroutineScope.deferredCache(block: suspend CoroutineScope.() -> T) : Deferred<T> = | |
async(start = CoroutineStart.LAZY) { | |
var result: T | |
while (true) { | |
try { | |
result = block() | |
break | |
} catch (e: Throwable) { | |
// failed -- retry | |
// todo: log exception somehow? | |
} | |
} | |
result | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment