Select in JavaScript
JavaScript doesn’t have built-in support for channels or goroutines like Go does. However, we can simulate similar behavior using Promises and async/await. Here’s how we can translate the select functionality:
In this JavaScript version, we use Promises to simulate the behavior of channels. The delay
function creates a Promise that resolves after a specified number of milliseconds, simulating the time.Sleep
in the original code.
Instead of goroutines, we create two Promises (c1
and c2
) that resolve after 1 and 2 seconds respectively.
The select
statement is simulated using Promise.race
, which returns a Promise that resolves or rejects as soon as one of the Promises in the iterable resolves or rejects.
We use a loop to receive both values, similar to the original code. After each iteration, we replace the resolved Promise with a never-resolving Promise to ensure we get both results.
To run this code:
Note that the total execution time is only ~2 seconds since both the 1 and 2 second delays execute concurrently.
This example demonstrates how to handle multiple asynchronous operations in JavaScript, waiting for whichever one completes first. While it’s not an exact equivalent of Go’s select, it provides similar functionality in terms of handling concurrent operations.