在Playframework中,如何在控制器内使用表单绑定执行异步作业?

在Playframework中,如何在控制器内使用表单绑定执行异步作业?,playframework,Playframework,在我的控制器中,我有一个简单的表单绑定和Slick数据库(删除)操作: def deleteApp = Action { implicit request => deletedAppForm.bindFromRequest.fold( formWithErrors => { Redirect(routes.AppController.index()) }, form => {

在我的控制器中,我有一个简单的表单绑定和Slick数据库(删除)操作:

def deleteApp = Action { implicit request =>
    deletedAppForm.bindFromRequest.fold(
        formWithErrors => {
            Redirect(routes.AppController.index())
        },
        form => {
            for (id <- form.idx) {
                appDao.delete(id) // database deleting with Slick
            }

            Redirect(routes.AppController.index())
        }
    )
}
当然,编译器也不喜欢它

我的问题是:在一般情况下,如何在控制器内使用表单绑定执行异步作业


感谢Steve,通常事务的异步部分是通过完成的。您可能希望尝试Action.async操作以实现非阻塞。我在过去使用JavaActions和Async尝试过类似的方法。在scala上,您可能希望参考这一点,看看这是否有帮助,Steve,通常事务的异步部分是通过。您可能希望尝试Action.async操作以实现非阻塞。我在过去使用JavaActions和Async尝试过类似的方法。在scala上,您可能想参考一下这一点,看看这是否有帮助。

“也许”这是正确的,尽管该函数可以工作,但我不确定它是否是真正的非阻塞

def deleteApp = Action.async { implicit request =>
    Future {
        deletedAppForm.bindFromRequest.fold(
            formWithErrors => {
                Redirect(routes.AppController.index())
            },
            form => {
                for (id <- form.idx) {
                    appDao.delete(id)
                }

                Redirect(routes.AppController.index())
            }
        )
    }
}
def deleteApp=Action.async{implicit request=>
未来{
deletedAppForm.bindFromRequest.fold(
formWithErrors=>{
重定向(routes.AppController.index())
},
形式=>{
对于(id“Maybe”这是正确的,尽管函数可以工作,但我不确定它是否是真正的非阻塞

def deleteApp = Action.async { implicit request =>
    Future {
        deletedAppForm.bindFromRequest.fold(
            formWithErrors => {
                Redirect(routes.AppController.index())
            },
            form => {
                for (id <- form.idx) {
                    appDao.delete(id)
                }

                Redirect(routes.AppController.index())
            }
        )
    }
}
def deleteApp=Action.async{implicit request=>
未来{
deletedAppForm.bindFromRequest.fold(
formWithErrors=>{
重定向(routes.AppController.index())
},
形式=>{

对于(id我认为,更好的解决方案是将appDao.delete细化为非阻塞。使用后:

def deleteApp = Action.async { implicit request =>
        deletedAppForm.bindFromRequest.fold(
            formWithErrors => {
                Future.successful(Redirect(routes.AppController.index()))
            },
            form => {
              Future.sequence(form.idx.map(appDao.delete(_)))
                    .map(_ => Redirect(routes.AppController.index()))                    
            }
        )
    }
}

我认为,更好的解决方案是将appDao.delete细化为非阻塞。使用后:

def deleteApp = Action.async { implicit request =>
        deletedAppForm.bindFromRequest.fold(
            formWithErrors => {
                Future.successful(Redirect(routes.AppController.index()))
            },
            form => {
              Future.sequence(form.idx.map(appDao.delete(_)))
                    .map(_ => Redirect(routes.AppController.index()))                    
            }
        )
    }
}

我做了一点修改,虽然代码运行了,但不确定是对是错。我做了一点修改,虽然代码运行了,但不确定是对是错。如果我更改为此,它将无法通过编译器。因为
操作。async
需要返回类型为
Future[Result]
,这就是为什么我将整个块包装在
Future
中。fold在两个变量中都返回Future[Result],因此必须编译此示例。哎呀,我错过了
forWithErrors
Future
部分。我认为您的答案应该是正确的,只需稍加修改:
Future.sequence(form.idx.map(appDao.delete()).map(=>Redirect(routes.AppController.index()))
,或者我的编译器出现错误。我没有控制台就写了,所以可能有一些打字错误。请您更改您原来的帖子,然后我就可以接受它。我认为这对像我这样的其他人会有帮助。如果我改为这样,它将无法通过编译器。因为
操作。async
需要返回类型为
Future[Result]
,这就是为什么我将整个块包装在
Future
中。fold在两个变量中都返回Future[Result],因此必须编译此示例。哎呀,我错过了
forWithErrors
Future
部分。我认为您的答案应该是正确的,只需稍加修改:
Future.sequence(form.idx.map(appDao.delete()).map(=>Redirect(routes.AppController.index())
,或者我的编译器出现错误。我在没有控制台的情况下编写,因此可能会有一些拼写错误。请您更改您原来的帖子,然后我就可以接受了。我想这对像我这样的人会有帮助。