You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
783 B

3 years ago
  1. package outwatchapp.util
  2. import scala.concurrent.Future
  3. import typings.paralleljs.ParallelOptions
  4. import typings.paralleljs.mod.{^ => Parallel}
  5. import scalajs.js
  6. import scalajs.js.|
  7. object ParallelDemo {
  8. val p =
  9. new Parallel(js.Array(1, 2, 3, 4, 5), ParallelOptions().setMaxWorkers(2))
  10. // val pr = new js.Promise()
  11. // p.map((_: Int) * 2)
  12. def toFuture[A](p: Parallel[A]): Future[A] = {
  13. val p2 = scala.concurrent.Promise[A]()
  14. p.`then`(
  15. { (v: A) =>
  16. p2.success(v)
  17. (): Unit | js.Thenable[Unit]
  18. },
  19. { (e: typings.std.Error) =>
  20. p2.failure(e match {
  21. case th: Throwable => th
  22. case _ => js.JavaScriptException(e)
  23. })
  24. (): Unit | js.Thenable[Unit]
  25. }
  26. )
  27. p2.future
  28. }
  29. }