我有下面的代码:斯卡拉,改变变量值内未来
val trxRequest: WSRequest = ws.url(route.transcationRoutes).withHeaders("Accept" -> "application/json")
val futureResponse: Future[WSResponse] = trxRequest.post(Json.obj("trxtype" -> trxType, "trxvariable" -> trxVariable, "account" -> account.toJson))
var responseStatus = 0 // I want to change this variable value
futureResponse.map {
response => {
logkey = (LoginKeyUtils.getEncryptedKey(accountId))
session.addSession(accountId -> logkey)
responseStatus = response.status // I change it here
println(responseStatus) // variable changed
}
}
Ok(responseStatus +"") //But here back to 0 again
我想改变responseStatus
值。它在地图{}内更改了值,但在外部地图时再次回到0。有什么不对?我是新手。
@Mikel,谢谢你这是解决方案,
val trxRequest: WSRequest = ws.url(route.transcationRoutes).withHeaders("Accept" -> "application/json")
val futureResponse: Future[WSResponse] = trxRequest.post(Json.obj("trxtype" -> trxType, "trxvariable" -> trxVariable, "account" -> account.toJson))
var responseStatus = 0
//Need to retrieve as future
val futureResult: Future[Any] = futureResponse.map {
response => {
logkey = (LoginKeyUtils.getEncryptedKey(accountId))
session.addSession(accountId -> logkey)
responseStatus = response.status;
println(responseStatus)
}
}
// wait for the result
Await.result(futureResult, 5000 millis)
Ok(responseStatus)
我们经常使用未来组合物/转化为这样的异步操作。如果你在这里使用'Await',注意会阻止播放主线程。如果使用大量的“等待”,则必须将主线程池配置为拥有更多线程。 – jilen