promise.cr icon indicating copy to clipboard operation
promise.cr copied to clipboard

Promise.all() result ordering is non-deterministic

Open Qard opened this issue 8 years ago • 0 comments

It'd be nice if Promise.all() had deterministic result ordering. Currently the result array is ordered by completion time not by position of associated promise in the input array. This is different from how JavaScript promises work, which maintain array position between input and output lists.

Here's an example of the issue:

promises = [] of Promise(Int32 | Nil)
10.times do |i|
  promises << Promise(Int32 | Nil).execute do
    sleep 10 - i
    i
  end
end
Promise(Int32 | Nil).all(promises)
  .then { |result| p result }
  .catch { |ex| puts ex.message }
  .await

This will not print [0,1,2,3,4,5,6,7,8,9] like one would expect, it will instead print [9,8,7,6,5,4,3,2,1,0]. If you change the sleep 10 - i to read sleep i it will result in a matching order due to the increasing sleep time.

Qard avatar Apr 07 '17 00:04 Qard