Skip to content

Commit 641436c

Browse files
davishmcclurgpitr-ch
authored andcommitted
Do not execute zipped promises
It's unnecessary to immediately execute promises that are chained together with `zip`. This feels like a bug to me, but I'm sure people are relying on the old behavior, so this is definitely a breaking change.
1 parent 50ed4a5 commit 641436c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/concurrent/promise.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def flat_map(&block)
387387
#
388388
# @return [Promise<Array>]
389389
def self.zip(*promises)
390-
zero = fulfill([], executor: ImmediateExecutor.new)
390+
zero = Promise.new(executor: ImmediateExecutor.new) { [] }
391391

392392
promises.reduce(zero) do |p1, p2|
393393
p1.flat_map do |results|

spec/concurrent/promise_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ def get_ivar_from_args(opts)
357357
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
358358
let(:promise3) { Promise.new(executor: :immediate) { [3] } }
359359

360+
it 'does not execute the returned Promise' do
361+
composite = promise1.zip(promise2, promise3)
362+
363+
expect(composite).to be_unscheduled
364+
end
365+
360366
it 'yields the results as an array' do
361367
composite = promise1.zip(promise2, promise3).execute.wait
362368

@@ -375,6 +381,12 @@ def get_ivar_from_args(opts)
375381
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
376382
let(:promise3) { Promise.new(executor: :immediate) { [3] } }
377383

384+
it 'does not execute the returned Promise' do
385+
composite = Promise.zip(promise1, promise2, promise3)
386+
387+
expect(composite).to be_unscheduled
388+
end
389+
378390
it 'yields the results as an array' do
379391
composite = Promise.zip(promise1, promise2, promise3).execute.wait
380392

0 commit comments

Comments
 (0)