Skip to content
Provides Async/Await and Promises for C++
Branch: master
Clone or download
Latest commit 5c8d7b9 May 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
.gitignore wip, initial passing tests May 15, 2019
README.md improve readme May 16, 2019
index.hxx refactor for co_await May 16, 2019
package.json refactor for co_await May 16, 2019

README.md

SYNOPSIS

Provides coroutine-based async/await and minimal promises.

ASYNC/AWAIT

USAGE

  • Any function that returns an Async<T> type becomes a coroutine.
  • Use co_return instead of return for coroutines.
  • Only coroutines can use co_await, and only coroutines are awaitable.
  • Awaitable functions have a get method to retrieve the value from the promise.

EXAMPLE

auto answer = []() -> Async<int> {
  //
  // ...possibly do something async here,
  // this could also be a regular function.
  //
  co_return 42;
};

auto question = []() -> Async<int> {
  //
  // await the promise created by the answer function.
  //
  auto n = co_await answer();

  //
  // use co_return instead of return!
  //
  co_return n;
};

auto v = question().get(); // v == 42

Refer to this paper for specifics about coroutines in C++.

PROMISE

Promise<int> p;
Timeout timeout;

timeout.start([&] {
  p.resolve(42);
}, 512);

auto v = p.await(); // blocks until resolved is called.
You can’t perform that action at this time.