2017-10-06 73 views
0

Hy!玩框架句柄未来[动作]

我有2个控制器函数返回一个Action。我有一个另一个控制器是这些控制器某事像之间进行选择:

def replace(i: Int, s:String): EssentialAction = ??? 
def asd: EssentialAction = { 
    if(true){ 
     replace(5,"asd") 
    } else { 
     replace(6,"asd") 
    } 
} 

但是,当该控制器是使用DB FUNC我得到某物像:

def asd: Future[EssentialAction] = { 
    Future(true).map{ bool => 
    if(bool){ 
     replace(5,"asd") 
    } else { 
     replace(6,"asd") 
    } 
    } 
} 

但路由器无法应对未来[EssentialAction] :(

我怎样才能重新包装未来[动作]只是控制器内的行动

回答

1

此代码编译:

package controllers 

import javax.inject._ 

import akka.stream.Materializer 
import play.api.mvc._ 

import scala.concurrent.ExecutionContext.Implicits.global 
import scala.concurrent.Future 

@Singleton 
class TestController @Inject()(cc: ControllerComponents)(implicit 
    val mat: Materializer) extends AbstractController(cc) { 

    def index = Action.async { request => 
    asd.flatMap(_.apply(request).run()) 
    } 

    def replace(i: Int, s:String): EssentialAction = ??? 

    def asd: Future[EssentialAction] = { 
    Future(true).map{ bool => 
     if(bool){ 
     replace(5,"asd") 
     } else { 
     replace(6,"asd") 
     } 
    } 
    } 
} 
+1

那.run()在最后......我真的很接近:D谢谢! – tg44

+0

不按预期工作需要一些更多的验证...我的一些测试失败后,此修改:( – tg44