Commit d0b905b1 authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook GitHub Bot

async::promiseWait

Summary:
Provide an Async wrapper for fibers::await, which blocks current task until promise is fulfilled.
This isn't a build block that is used in very common scenario, so prefered to put it in its own file.

Differential Revision: D22135318

fbshipit-source-id: 753dbefc4ac9a907f5b1f656f70291c88187b5e8
parent 72e91069
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/fibers/async/Async.h>
namespace folly {
namespace fibers {
namespace async {
/**
* Async annotated wrapper around fibers::await
*/
template <typename F>
auto promiseWait(F&& func) {
// Call into blocking API
return Async{folly::fibers::await(std::forward<F>(func))};
}
} // namespace async
} // namespace fibers
} // namespace folly
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <folly/fibers/async/Async.h> #include <folly/fibers/async/Async.h>
#include <folly/fibers/async/Baton.h> #include <folly/fibers/async/Baton.h>
#include <folly/fibers/async/Future.h> #include <folly/fibers/async/Future.h>
#include <folly/fibers/async/Promise.h>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#if FOLLY_HAS_COROUTINES #if FOLLY_HAS_COROUTINES
...@@ -114,6 +115,18 @@ TEST(AsyncTest, asyncBaton) { ...@@ -114,6 +115,18 @@ TEST(AsyncTest, asyncBaton) {
.getVia(&evb)); .getVia(&evb));
} }
TEST(AsyncTest, asyncPromise) {
folly::EventBase evb;
auto& fm = getFiberManager(evb);
fm.addTaskFuture([&]() {
auto res = async::await(
async::promiseWait([](Promise<int> p) { p.setValue(42); }));
EXPECT_EQ(res, 42);
})
.getVia(&evb);
}
TEST(AsyncTest, asyncFuture) { TEST(AsyncTest, asyncFuture) {
folly::EventBase evb; folly::EventBase evb;
auto& fm = getFiberManager(evb); auto& fm = getFiberManager(evb);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment