let print1 = new Promise((resolve) => {
for (let i = 0; i < 10; i++) console.log(i)
resolve();
})
let print2 = new Promise((resolve) => {
for (let i = 0; i < 10; i++) console.log(-i)
resolve();
})
Promise.all([print1, print2])
require 'concurrent'
print1 = Concurrent::Promises.future {
10.times { |i| puts i }
}
print2 = Concurrent::Promises.future {
10.times { |i| puts -i }
}
Concurrent::Promises.zip(print1, print2).wait!
この2つのコードはとても似ているが、実は結構違った動きをする。
$ node a.js
0
1
2
3
4
5
6
7
8
9
-0
-1
-2
-3
-4
-5
-6
-7
-8
-9
$ bundle exec ruby a.rb
0
1
2
0
-1
-2
-3
-4
-5
3
4
-6
-7
5
6
7
8
9
-8
-9