On Thu, 17 May 2018 08:25:53 GMT, boolangery wrote:


How to wait for a Future object asynchronously ?

I understand that getResult() is a blocking call.

Is there something more convenient like a callback when the value became available ?


If a callback is desired, I'd recommend to simply use runTask or runWorkerTask directly:

// same thread:
runTask({ callback(computeValue()); });

// worker thread:
auto callback_task = runTask({ callback(receiveOnly!T()); }
runWorkerTask((t) { t.tid.send(computeValue()); }, callback_task);

The idea behind Future!T is that it (ideally) behaves like a value of T itself, just that the value is computed in the background. The current API is still far from that ideal (explicit getResult, no support for operations chaining, e.g. Future!int a = ..., b = ...; Future!int c = a + b;, so that c is also computed in the background), but that's at least the goal.

So using a callback is in that sense really a different programming paradigm, but on the other hand it should be okay to add an overload of getResult that takes a void delegate(T), which is then called asynchronously in the thread that called getResult. I've opened a ticket for this: #2166