Module: Temporalio::Workflow::Async

Defined in:
lib/temporalio/workflow/async.rb

Class Method Summary collapse

Class Method Details

.all(*futures) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/temporalio/workflow/async.rb', line 18

def self.all(*futures)
  Future.new do |future, resolve, _reject|
    future.on_cancel { futures.each(&:cancel) }

    futures.each do |f|
      f.then do
        # Resolve the aggregate once all futures have been fulfilled (resolved or rejected)
        resolve.call(nil) if futures.none?(&:pending?)
      end
    end
  end
end

.any(*futures) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/temporalio/workflow/async.rb', line 31

def self.any(*futures)
  # This future is never rejected
  Future.new do |future, resolve, _reject|
    future.on_cancel { futures.each(&:cancel) }

    futures.each do |f|
      # Resolve the aggregate once the first future fulfills (resolved or rejected)
      # NOTE: The the first completed future will be the resolved value and all subsequent
      #       calls to `resolve` will have no effect.
      f.then { resolve.call(f) }
    end
  end
end

.run(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/temporalio/workflow/async.rb', line 6

def self.run(&block)
  Future.new do |future, resolve, reject|
    Fiber.new do
      Future.current = future
      result = block.call
      resolve.call(result)
    rescue StandardError => e
      reject.call(e)
    end.resume
  end
end