Asynchronous Programming with Seastar

Nadav Har’El - nyh@ScyllaDB.com

Avi Kivity - avi@ScyllaDB.com

Back to table of contents. Previous: 21. Debugging a Seastar program. Next: 23. Memory allocation in Seastar.

22 Promise objects

As we already defined above, An asynchronous function, also called a promise, is a function which returns a future and arranges for this future to be eventually resolved. As we already saw, an asynchronous function is usually written in terms of other asynchronous functions, for example we saw the function slow() which waits for the existing asynchronous function sleep() to complete, and then returns 3:

seastar::future<int> slow() {
    using namespace std::chrono_literals;
    return seastar::sleep(100ms).then([] { return 3; });
}

The most basic building block for writing promises is the promise object, an object of type promise<T>. A promise<T> has a method future<T> get_future() to returns a future, and a method set_value(T), to resolve this future. An asynchronous function can create a promise object, return its future, and the set_value method to be eventually called - which will finally resolve the future it returned.

CONTINUE HERE. write an example, e.g., something which writes a message every second, and after 10 messages, completes the future.

Back to table of contents. Previous: 21. Debugging a Seastar program. Next: 23. Memory allocation in Seastar.