From: Hans Fugal Date: Wed, 27 May 2015 01:10:15 +0000 (-0700) Subject: Promise::isFulfilled() X-Git-Tag: v0.41.0~4 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=323c9a330ba0c86599ed3d4781cdd94ee6c4b573;p=folly.git Promise::isFulfilled() Summary: See task Test Plan: runtests Reviewed By: jsedgwick@fb.com Subscribers: exa, folly-diffs@, jsedgwick, yfeldblum, chalfant FB internal diff: D2101549 Tasks: 7225286 Signature: t1:2101549:1432688294:5fb9d7370c38c1392429a09ba48b131cac16647d --- diff --git a/folly/futures/Promise-inl.h b/folly/futures/Promise-inl.h index 7019b720..245b0d6f 100644 --- a/folly/futures/Promise-inl.h +++ b/folly/futures/Promise-inl.h @@ -130,4 +130,12 @@ void Promise::setWith(F&& func) { setTry(makeTryWith(std::forward(func))); } +template +bool Promise::isFulfilled() { + if (core_) { + return core_->hasResult(); + } + return true; +} + } diff --git a/folly/futures/Promise.h b/folly/futures/Promise.h index 0ee979ea..27b20bd8 100644 --- a/folly/futures/Promise.h +++ b/folly/futures/Promise.h @@ -99,6 +99,8 @@ public: template void setWith(F&& func); + bool isFulfilled(); + private: typedef typename Future::corePtr corePtr; diff --git a/folly/futures/test/FutureTest.cpp b/folly/futures/test/FutureTest.cpp index f6c295e0..58619d69 100644 --- a/folly/futures/test/FutureTest.cpp +++ b/folly/futures/test/FutureTest.cpp @@ -1914,3 +1914,20 @@ TEST(Map, Basic) { EXPECT_TRUE(collect(fs2).isReady()); } + +TEST(Promise, isFulfilled) { + Promise p; + + EXPECT_FALSE(p.isFulfilled()); + p.setValue(42); + EXPECT_TRUE(p.isFulfilled()); +} + +TEST(Promise, isFulfilledWithFuture) { + Promise p; + auto f = p.getFuture(); // so core_ will become null + + EXPECT_FALSE(p.isFulfilled()); + p.setValue(42); // after here + EXPECT_TRUE(p.isFulfilled()); +}