Function async

Starts an asynchronous computation and returns a future for the result value.

If the supplied callable and arguments are all weakly isolated, vibe.core.core.runWorkerTask will be used to perform the computation. Otherwise, vibe.core.core.runTask will be used.

Prototype

Future!(ReturnType!CALLABLE) async(CALLABLE, ARGS...)(
  CALLABLE callable,
  ARGS args
)
if (is(typeof(callable(args)) == ReturnType!CALLABLE));

Parameters

NameDescription

callable

A callable value, can be either a function, a delegate, or a user defined type that defines an opCall.

args

Arguments to pass to the callable.

Returns

Returns a Future object that can be used to access the result.

See also

isWeaklyIsolated

Example

import vibe.core.core;
import vibe.core.log;

void test()
{
	static if (__VERSION__ >= 2065) {
	auto val = async({
		logInfo("Starting to compute value in worker task.");
		sleep(500.msecs); // simulate some lengthy computation
		logInfo("Finished computing value in worker task.");
		return 32;
	});

	logInfo("Starting computation in main task");
	sleep(200.msecs); // simulate some lengthy computation
	logInfo("Finished computation in main task. Waiting for async value.");
	logInfo("Result: %s", val.getResult());
	}
}

Authors

Sönke Ludwig

Copyright

© 2013-2014 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.