Skip to content

Instantly share code, notes, and snippets.

@elizarov
Created September 27, 2018 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elizarov/d16664152fa65f6bd794319a5b1ed9ab to your computer and use it in GitHub Desktop.
Save elizarov/d16664152fa65f6bd794319a5b1ed9ab to your computer and use it in GitHub Desktop.
DeferredCache
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