如何将流的内容放入val? [英] How to put contents of stream into a val?

查看:114
本文介绍了如何将流的内容放入val?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的信息流:

def myStream[T: AS: MAT](source: Source[T, NotUsed]): Future[Seq[T]] = {
    return source.runWith(Sink.seq)
}

def myMethod(colorStream: Source[Color, NotUsed]) {
  val allColors = myStream(colorStream).map(_.toList)

  //how can I actually extract the things from allColors
  //so that I can call my method below? myOtherMethod

  if I do println(allColors.map(println _)) I can print the elements fine
}

def myOtherMethod(colors: Seq[Color] = List.empty()) {
 ...
}


推荐答案

allColors是未来。您需要访问将来要包装的内容才能访问colours:Seq [Color]。试试这个:

allColors is a Future. You need to access what the future is wrapping to access the colours:Seq[Color]. Try this:

allColors.onComplete{
  case Success(list) => myOtherMethod(list)
  case Failure(err) => //handle the error
}

这篇关于如何将流的内容放入val?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆