sync with dev

This commit is contained in:
Ryan ZHAO
2021-02-01 09:39:14 +11:00
parent 59ae46b300
commit 190badd9c0
6 changed files with 64 additions and 55 deletions

View File

@@ -45,25 +45,25 @@ fun <V, E : Throwable> Promise<V, E>.recover(callback: (exception: E) -> V): Pro
}
fun <V, E> Promise<V, E>.successBackground(callback: (value: V) -> Unit): Promise<V, E> {
Thread {
ThreadUtils.queue {
try {
callback(get())
} catch (e: Exception) {
Log.d("Loki", "Failed to execute task in background: ${e.message}.")
}
}.start()
}
return this
}
fun <V> Promise<V, Exception>.timeout(millis: Long): Promise<V, Exception> {
if (this.isDone()) { return this; }
val deferred = deferred<V, Exception>()
Thread {
ThreadUtils.queue {
Thread.sleep(millis)
if (!deferred.promise.isDone()) {
deferred.reject(TimeoutException("Promise timed out."))
}
}.start()
}
this.success {
if (!deferred.promise.isDone()) { deferred.resolve(it) }
}.fail {