Search code examples
ruby-on-railsrubyes6-promisemethod-chaining

Is promise concept available in ruby?


I'm Just wondering, is there a chaining concept in ruby.
I wanted to execute series of async tasks or methods one after the other. Is it possible?


Solution

  • You might want to create a process class, something like:

    class MyProcess
    
      PROCESS_STEPS = %w(
        step_one
        step_two
        step_three
      )
    
      class << self 
    
        def next_step
          new.next_step
        end
    
      end # Class Methods
    
      #======================================================================
      # Instance Methods
      #======================================================================
    
        def next_step
          PROCESS_STEPS.each do |process_step|
            send(process_step) if send("do_#{process_step}?")
          end
        end
    
        def step_one
          # execute step one task
        end
    
        def do_step_one?
          # some logic
        end
    
        def step_two
          # execute step two task
        end
    
        def do_step_two?
          # some logic
        end
    
        def step_three
          # execute step three task
        end
    
        def do_step_three?
          # some logic
        end
    
    end
    

    You would probably put that in:

    app 
     |- processes
     |   |- my_process.rb
    

    Then, at the end of each task, do something like:

    MyProcess.next_step