Skip to main content
Module

x/async_channels/channel.ts>select

Inspired by Go & Clojure Channels, async_channels provides channels as an asynchronous communication method between asynchronous functions.
Go to Latest
function select
import { select } from "https://deno.land/x/async_channels@v1.0.0-alpha45/channel.ts";

select takes a list of channel operations, and completes at-most one of them (the first operation that is ready).

If the default option is provided, and no operation is immediately read, then all the operations will be aborted and the default value is returned.

Type Parameters

T
optional
TDefault = never

Parameters

ops: SelectOperation<T>[]

A list of channel operations. Each item in this list can be either a receiver or a tuple of a sender and the value to send.

optional
options: SelectOptions<TDefault> | Omit<SelectOptions<TDefault>, "default">

The options for select. Note: undefined is considered a valid value for default, so if you want to wait for one of the operations, omit default from the options struct.

Returns

Promise<SelectResult<T, TDefault>>

If default is provided and no operation was ready, then the tuple [default, undefined] is returned.

If a receive operation is completed, then the tuple [T, Receiver<T>] is returned (where T is the value received, and Receiver<T> is the receiver channel that returned it).

If a send operation is completed, then the tuple [true, Sender<T>] is returned (where Sender<T> is the sender channel that we sent the value with).