我不确定我明白你的意思…但我认为你在找这样的东西:
def myMethod(shadesFuture: Future[Seq[Shade]]) : Future[ColorShade] = for {
shades <- shadesFuture
color <- colorFuture.map(...) // whatever the transformations you wanted
transformedColor = ColorParser(color.utf8String.trim) // or you can do them like this
colorShade = ColorShade(color, shades)
// whatever you wanted to do with it here
_ = someOtherMethod(colorShade.c, colorShade.shades)
} yield colorShade // or whatever you need to return. The result of this is `Future[ColorShade]`
这被称为“为了理解”,在句法上对一堆嵌套的
.flatMap
电话。你也可以把它写得很明确(这不完全是理解的东西,但功能上是等价的):
shadesFuture
.flatMap { shades =>
colorFuture
.map(...) // transform, whatever
.map(ColorShade(_, shades)
}