<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="160617" data-post-id="160617">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="gregvaughn" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregvaughn/120/954_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  gregvaughn
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m sorry I don’t have some example to share, but I view it as some basics of functional programming. Here’s some pseudo code (caution: I have not executed it) to think about</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Enum.reduce(data_list, (:initial_state, nil), &amp;transition/2)

def transition(data_element, {:initial_state, accumulated_data}) do
  #do something and return {new_state, new_accumulated_data}
end

def transition(data_element, {:state2, accumulated_data}) do
  #do something else and return {new_state, new_accumulated_data}
end

# ... plus as many more transition/2 function clauses you need
</code></pre>
<p>Now the result of the <code>Enum.reduce</code> is <code>{final_state, final_accumulated_data}</code> and if you use the <code>async_stream</code> approach I suggested, it will be sent to the calling process automatically. There is no need to store this (in process state or ETS) and later retrieve with some <code>:get_job_result</code> message. That is the whole advantage of the <code>async_stream</code> approach.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="160617" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/worker-with-state-vs-cache-worker/28504/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-160617" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="160617"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="160625" data-post-id="160625">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="smanza" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  smanza
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you very much <a class="mention" href="/u/gregvaughn" rel="nofollow">@gregvaughn</a> .<br>
Nice way to do it <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>But I my use case, I don’t known if <code>async_stream</code> is the right thing to do, because I need to retrieve the result asynchronously later not after the job completion but when I will receive to check and compare the jobs results.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="160625" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/worker-with-state-vs-cache-worker/28504/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-160625" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="160625"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="160636" data-post-id="160636">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ityonemo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/120/11341_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ityonemo
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I would say when to use gen_statem:</p>
<ol>
<li>you’re modeling an external “real thing” that you have limited control over, or stateful communications protocol.</li>
<li>there are recurrent events that must be asynchronous (eg you have to check on your external thing and you don’t know when the response will come back)</li>
<li>your model is not transient (if your process crashes you want to recover, and you can’t just throw it away and start from scratch)</li>
</ol>
<p>If all do not apply, do not use gen_statem.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="160636" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/worker-with-state-vs-cache-worker/28504/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-160636" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="160636"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>