2017-10-06 51 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