1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

concurrent-rubyのConcurrent::PromisesはJavaScriptのPromiseと結構違う

Posted at
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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?