今天看到了flatMap的变体flatMapM

flatMapM{

_.map(getUserWithKey).getOrElse(Future.failed(UnavailableUserKey))……


点进去后看发现:

/**
*LikeflatMapbutallowstheflatMapfunctiontoexecuteasynchronously.
*
*@paramftheasyncfunctiontoproduceanewbodyparserfromtheresultofthisbodyparser
*@paramecThecontexttoexecutethesuppliedfunctionwith.
* Thecontextispreparedonthecallingthread.
*@returnthetransformedbodyparser
*@see[[flatMap]]
*@see[[play.api.libs.iteratee.Iteratee#flatMapM]]
*/
defflatMapM[B](f:A=>Future[BodyParser[B]])(implicitec:ExecutionContext):BodyParser[B]={
//prepareexecutioncontextasbodyparserobjectmaycrossthreadboundary
implicitvalpec=ec.prepare()
newBodyParser[B]{
defapply(request:RequestHeader)=self(request).flatMapM{
caseRight(a)=>
f(a).map{_.apply(request)}(pec)
caseleft=>
Future.successful{
Done[Array[Byte],Either[Result,B]](left.asInstanceOf[Either[Result,B]])
}
}(pec)
overridedeftoString=self.toString
}
}