Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rtmigo/later_kt
Lightweight asynchronous values for Kotlin. Serves the same purpose as Future, Deferred, Promise. Great for procrastinators
https://github.com/rtmigo/later_kt
async await callback deferred future jvm kotlin promise
Last synced: about 2 months ago
JSON representation
Lightweight asynchronous values for Kotlin. Serves the same purpose as Future, Deferred, Promise. Great for procrastinators
- Host: GitHub
- URL: https://github.com/rtmigo/later_kt
- Owner: rtmigo
- License: mit
- Created: 2022-09-22T19:11:11.000Z (over 2 years ago)
- Default Branch: staging
- Last Pushed: 2022-09-27T01:13:09.000Z (over 2 years ago)
- Last Synced: 2023-03-11T08:51:57.819Z (almost 2 years ago)
- Topics: async, await, callback, deferred, future, jvm, kotlin, promise
- Language: Kotlin
- Homepage:
- Size: 140 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Generic badge](https://img.shields.io/badge/Kotlin-1.7-blue.svg)
![Generic badge](https://img.shields.io/badge/JVM-8-blue.svg)# [later](https://github.com/rtmigo/later_kt)
A `Later` represents a potential value, that will be available at some
time in the future.- `.value` returns the value, or throws if the value is not set yet
- `.isComplete` returns `true`, if the value is set
- `.await()` waits until the value is set
The object does not provide concurrency or task queuing. It just fires
callbacks as lightly as possible while being thread safe.- `.onComplete`, `.onSuccess`, `.onError` set callbacks to be run when Later
is completed (or run the callbacks immediately, if Later is already completed)
- `.map` maps the value to other Later valueThe object is somewhat similar to
[Deferred](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/),
[Future](https://api.dart.dev/be/175791/dart-async/Future-class.html) and
[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).# Install
#### settings.gradle.kts
```kotlin
sourceControl {
gitRepository(java.net.URI("https://github.com/rtmigo/later_kt.git")) {
producesModule("io.github.rtmigo:later")
}
}
```#### build.gradle.kts
```kotlin
dependencies {
implementation("io.github.rtmigo:later") {
version { branch = "staging" }
}
}
```# Basic example
```kotlin
import io.github.rtmigo.later.*fun slowComputation(x: Int): Later {
val result = Later.completable()
thread {
// one second later assign value to the returned object
sleep(1000)
result.value = x * 2
}
return result // return immediately
}fun main() {
// print "16" after one second
println(slowComputation(8).await())
}
```# Creating a Later
`Later.completable()` creates an object without `.value`. The value must be
assigned before it can be read.```kotlin
val a = Later.completable()
assert(a.isComplete == false)a.value = 5
assert(a.isComplete == true)
assert(a.value == 5)
```By calling `Later.value(v)` or `v.asLater()` we create an immutable `Later`,
with `v` as value.```kotlin
val c = Later.value(7)assert(c.isComplete == true)
assert(c.value == 7)
```# Async callbacks
```kotlin
// init
val a = Later.completable()
a.onSuccess { println("What is $it?!") }// assigning the value will trigger the callback
a.value = "love"// What is love?!
```We can set multiple callbacks for the same `Later`.
```kotlin
val a = Later.completable()
a.onSuccess { println("What is $it?!") }
a.onSuccess { println("Is $it great?!") }
a.value = "Britain"// What is Britain?
// Is Britain great?
```When value is already set, callbacks are run immediately.
```kotlin
val iam = Later.value("Groot")a.onSuccess { println("You are $it") }
// You are Groot
```We can use `Unit` as value if all we need is a callback.
```kotlin
val kindaEvent = mutableLater()
kindaEvent.onSuccess { println("Kinda callback") }kindaEvent.value = Unit
// Kinda callback
```# Mapping
We can specify later transformations for a later value.
`map` method receives the previous value and returns a new `Later`.
```kotlin
val a = Later.completable() // a is CompletableLater
val b = a.map { "The number is $it".asLater() } // b is Later
val c = b.map { (it.uppercase()+"!").asLater() } // c is Later// None of the objects have a value yet. Attempting to read `.value`
// will throw an exception. But we can assign value to `a`:a.value = 5
println(a.value) // 5
println(b.value) // The number is 5
println(c.value) // THE NUMBER IS 5!
```# License
Copyright © 2022 Artyom IG
Licensed under the [MIT License](LICENSE)