Timeout Using Select

Learn about the timeout pattern thatuses select.

Overview of timeout using select

The select statement randomly selects a case from all the case statements that are ready to execute. As the name suggests, the timeout pattern that uses select exits the select clause once the timer completes. Let’s suppose we’re running an infinite loop but need to end it after x seconds. In that case, the timeout pattern that uses select is very useful.

Let’s take another scenario. Say we are organizing a quiz, and the time to answer each question is one minute. We can easily use this pattern.

Timeout for an entire conversation

Let’s look at the following code block. The select clause will exit after one second, no matter how many values the channel holds.

Get hands-on with 1400+ tech skills courses.