Project

General

Profile

Feature #13821 ยป wait.rb

use-case - cremes (Chuck Remes), 08/16/2017 05:13 PM

 
1
require_relative 'lib/async-await'
2

    
3
Thread.abort_on_exception = true
4

    
5
# Wait docs...
6
class Wait
7
  include AsyncAwait
8

    
9
  def dofirst
10
    async do
11
      puts 'Synchronously print dofirst.'
12
      result = await { dosecond }
13
      puts 'dosecond is complete'
14
      result
15
    end
16
  end
17

    
18
  def dosecond
19
    async do
20
      puts 'Synchronously print dosecond from async task.'
21
      slept = await { sleep 3 }
22
      puts 'Sleep complete'
23
      slept
24
    end
25
  end
26

    
27
  def run
28
    task = dofirst
29
    puts 'Received task'
30
    p AsyncAwait::Task.await(task)
31
  end
32
end
33

    
34
Wait.new.run
35

    
36
# Expected output:
37
# Synchronous print dofirst.
38
# Received task
39
# Synchronously print dosecond from async task.
40
# Sleep complete
41
# dosecond is complete
42
# 3
43