An after clause gives receive a bounded wait. When no mailbox message matches its patterns within the specified time in milliseconds, the after branch executes instead of waiting indefinitely. section 5.2.2
Receive processes messages and timeout behavior in this order:
- Searches the mailbox for the oldest message matching a receive pattern. section 5.2.2
- Executes the matching branch and returns its last expression. section 5.2.2
- Continues waiting when messages are absent or do not match, until the after timeout expires. section 5.2.2
Bounded receive
receive do message -> IO.inspect(message) after 5000 -> IO.puts("message not received") end With no matching message, the timeout branch prints its fallback after 5,000 milliseconds. section 5.2.2
iex(1)> receive do
message -> IO.inspect(message)
after
5000 -> IO.puts("message not received")
end
message not received #1
Use the interactive example to see receive avoid an indefinite block and execute its timeout branch. section 5.2.2
