From b64756acca2eb942c97a416850ce5ab95a544d3e Mon Sep 17 00:00:00 2001 From: Tanvi Jagtap <139093547+tanvi-jagtap@users.noreply.github.com> Date: Sat, 1 Feb 2025 21:56:35 -0800 Subject: [PATCH] [PH2][Documentation][Promise] Seq (#38339) [PH2][Documentation][Promise] Seq Closes #38339 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/38339 from tanvi-jagtap:ph2_comb_seq_doc b1d393ab21dafebf8685195f9cd49ca531d97f18 PiperOrigin-RevId: 722257003 --- src/core/lib/promise/seq.h | 47 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/core/lib/promise/seq.h b/src/core/lib/promise/seq.h index 8eb16174c3515..768eddb5b2305 100644 --- a/src/core/lib/promise/seq.h +++ b/src/core/lib/promise/seq.h @@ -28,6 +28,47 @@ namespace grpc_core { +// Seq Promise combinator. +// +// Seq stands for sequence. +// +// Input : +// 1. The seq combinator needs minimum one promise as input. +// 2. The first input to seq combinator is a promise. +// 3. The remaining inputs to seq combinator are promise factories. The input +// type of the Nth functor should be the return value of the (N-1)th promise. +// +// Return : +// Polling the Seq Promise combinator returns Poll where T is the type +// returned by the last promise in the list of input promises. +// +// Polling the Seq combinator works in the following way : +// Run the first promise. If it returns Pending{}, nothing else is executed. +// If the first promise returns a value, pass this result to the second functor, +// and run the returned promise. If it returns Pending{}, nothing else is +// executed. If it returns a value, pass this result to the third, and run the +// returned promise. etc. Return the final value. +// +// If any of the promises in the Seq chain returns a failure status, Seq will +// still proceed with the execution of the remaining promises. If you want the +// execution to stop when a failure status is received, use the TrySeq +// combinator instead. +// +// Promises in the Seq combinator are run in order, serially and on the same +// thread. +// +// Example : +// +// TEST(SeqTest, TwoThens) { +// auto initial = [] { return std::string("a"); }; +// auto next1 = [](std::string i) { return [i]() { return i + "b"; }; }; +// auto next2 = [](std::string i) { return [i]() { return i + "c"; }; }; +// EXPECT_EQ(Seq(initial, next1, next2)(), Poll("abc")); +// } +// +// For a complete understanding of all possible uses and nuances of Seq look at +// ThreeTypedPendingThens in file seq_test.cc + namespace promise_detail { template @@ -76,12 +117,6 @@ using SeqIter = BasicSeqIter; } // namespace promise_detail -// Sequencing combinator. -// Run the first promise. -// Pass its result to the second, and run the returned promise. -// Pass its result to the third, and run the returned promise. -// etc -// Return the final value. template GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline F Seq(F functor) { return functor;