golo-lang
golo-lang copied to clipboard
Future: get() with (long timeout, TimeUnit unit)
Hello 🌍
gololang.concurrent.async.Futurehas not agetmethod to handle a timeout- so
gololang.concurrent.async.AssignedFuturedoes not implement this method
What I'm doing instead:
let concurrentTask = |task, timeout| {
let executor = Executors.newSingleThreadExecutor()
return trying({
let future = executor: submit(asInterfaceInstance(java.util.concurrent.Callable.class, {
return task()
}))
let res = future: get(timeout, MILLISECONDS())
executor: shutdown()
return res
})
}
concurrentTask({
sleep(2000_L)
return "Hello 🌍"
}, 1000_L)
: either(
recover=|error| {
println("😡")
},
mapping=|result| {
println(result)
}
)