crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.5k stars 1.62k forks source link

Update example code for `::spawn` with `WaitGroup` #15191

Closed BigBoyBarney closed 1 week ago

BigBoyBarney commented 1 week ago

Currently, the example in the docs for ::spawn still uses the old way of handling fibers that return nil, namely:

# Write "1" every 1 second and "2" every 2 seconds for 6 seconds.

ch = Channel(Nil).new

spawn do
  6.times do
    sleep 1.second
    puts 1
  end
  ch.send(nil)
end

spawn do
  3.times do
    sleep 2.seconds
    puts 2
  end
  ch.send(nil)
end

2.times { ch.receive }

This PR aims to modernise the example using WaitGroup.