2013-06-24 13 views
5

नीचे दिया गया कोड संकलित नहीं करता है और मुझे नहीं पता कि इसे कैसे ठीक किया जाए।Play 2.1.1 async अनुरोधों और वायदा का उपयोग कैसे करें

def doAsync(n: Int) = Action { 
    import scala.concurrent.ExecutionContext.Implicits.global 

    Async { 
    val f1 = Future.successful(n) 

    f1.map(x => x match { 
     case 10 => Ok("first") 
     case _ => { 
     val f2 = Future.successful(n) 
     f2.map(y => Ok("second")) 
     } 
    }) 
    } 
} 

लाइन f2.map(y => Ok("second"))संकलन त्रुटि पैदा करता है:

प्रकार मेल नहीं खाता; पाया: scala.concurrent.Future [play.api.mvc.SimpleResult [स्ट्रिंग]] की आवश्यकता: play.api.mvc.Result

कोर "business logic" is: f2 केवल if n is not 10 चलाना चाहिए।

उत्तर

5

एफ 1 परिणाम का भविष्य होने जा रहा है।

आप मामले 10 के लिए परिणाम लौट रहे हैं, और परिणामस्वरूप भविष्य का भविष्य। आपको परिणाम को एक ही नियम में रखना होगा। आज़माएं:

f1 flatMap {x => x match { 
    case 10 => Future.successful(Ok("first")) 
    case _ => { 
    val f2 = Future.successful(n) 
    f2.map(y => Ok("second")) 
    } 
} 
संबंधित मुद्दे