I have a set of tasks that I want to perform sequentially in some background thread, as a result of which each task is transferred to the next, and the chain does not work if any link in the chain fails.
For the argument, suppose that each task is an object with an exec method that returns a value, although they can equally well be proc or lambdas.
Now I have something like:
promise = array_of_tasks.inject(nil) do |promise, task| if promise promise.then { |prev_result| task.exec(prev_result) } else Concurrent::Promise.new { task.exec } end end promise.on_success { |last_result| log.info("Success: #{last_result} ")} promise.rescue { |reason| log.error("Failure: #{reason}")}
Is there a shorter way to do this, either in the Promise API or elsewhere in Concurrent Ruby? This seems like a pretty basic operation, but I don't see the existing method that does this.
(Note: if there is no such method, is there a known name for this template in the world of futures and promises? That is, if I write the method myself, are there any obvious names for it?)
source share