my.await

Members

Functions

async
void async(void delegate() task)

Creates an async task. An async task is a task that will be running in a separate fiber, independent from the current fiber.

await
T await(T task)

Runs the argument in a separate task, waiting for the result.

startScheduler
void startScheduler(void delegate() firstTask)

Starts the scheduler.

Meta

Authors

Sebastiaan de Schaetzen

Joakim Brännström (joakim.brannstrom@gmx.com) (modifications)

*All** credit goes to Sebastiaan. It is only copied here for convenience.

A simple to use async/await library.

dawait - A simple to use async/await library

This library provides a very easy-to-use async/await library for D. It consists of only three functions: async, await, and startScheduler. The library is build on top of D's fibers and allows for easier cooperative multitasking.

Functionality

|Function|Description| |--------|-----------| |startScheduler(void delegate() callback)| Starts the scheduler with an initial task.| |async(void delegate() callback)|Runs the given delegate in a separate fiber.| |await(lazy T task)|Runs the expression in a separate thread. Once the thread has completely, the result is returned.|

Code Example

import std.stdio;

int calculateTheAnswer() {
    import core.thread : Thread;
    Thread.sleep(5.seconds);
    return 42;
}

void doTask() {
    writeln("Calculating the answer to life, the universe, and everything...");
    int answer = await(calculateTheAnswer());
    writeln("The answer is: ", answer);
}

void main() {
    startScheduler({
        doTask();
    });
}