PlayScala实战 - 如何优雅地取出多层Future中的结果?

1 问题背景

我们先看一下Play中Action代码的基本结构:

def greeting = Action.async { implicit request =>
  for{
    r1 <- Future.successful("result")
    r2 <- Future.successful("result")
  } yield {
    Ok("hello")
  }
}

Action的body部分返回类型是Future[Result]。如果只是简单的数据库查询操作,使用for表达式就足够了,就像上面那样。但是如果在yield部分还需要做一些异步的业务处理,问题就出现了,例如下面这个保存商品信息的Action代码:

def doEditProduct(_id: String) = Action.async { implicit request =>
  for{
    product <- fetchProductAsync
    other    <- otherInfoAsync
  } yield {
    //比较一下,看看哪些信息在页面上被修改了
    //...
    updateProductAsync()
    Redirect(routes.ProductController.editProduct(_id))
  }
}

首先利用for语句取出异步的product结果,然后对比一下页面数据和数据库中的差异,这一步在很多时候是需要的,例如记录修改日志,然后异步更新至数据库,接着将页面跳转至该商品的编辑页面。那么问题来了,跳转至编辑页面后用户看到的是编辑前的结果还是编辑后的结果?呵呵,只能看运气了!很可能在更新操作未完成之前,编辑页面已经刷出来了。面对这种情况,你很可能会说同步等待updateProductAsync()的结果返回呗,千万别这么干,高并发时你的线程很快就耗尽了,另外updateProductAsync()操作之后可能还会有其它的异步更新操作,就像这样:

def doEditProduct(_id: String) = Action.async { implicit request =>
  for{
    product <- fetchProductAsync
    other    <- otherInfoAsync
  } yield {
    //比较一下,看看哪些信息在页面上被修改了
    //...
    updateProductAsync().map{ writeResult => 
       if(...){ asyncOperate1() } else { asyncOperate2() }
    }
    Redirect(routes.ProductController.editProduct(_id))
  }
}

如果asyncOperate1() 和asyncOperate2()也会更新商品信息, 你可能就要骂娘了...

2 解决方案

其实上面的问题可以归结为本文的标题,即如何从多层Future中取出最终的执行结果。其实for语句可以很容易解决这个问题:

def doEditProduct(_id: String) = Action.async { implicit request =>
    val multiLevelsFuture = 
      for{
        product <- fetchProductAsync
        other    <- otherInfoAsync
      } yield {
        updateProductAsync().map{ writeResult => 
           asyncOperate1.map{ writeResult1 =>
           	 Redirect(routes.ProductController.editProduct(_id))
           }
        }    
      }

  for{
    level1Future <- multiLevelsFuture
    level2Future <- level1Future
    finalResult  <- level2Future
  } yield {
  	finalResult
  } 
}

for语句最终会被编译器翻译为map和flatMap,我们索性直接上,而且代码更简洁:

def doEditProduct(_id: String) = Action.async { implicit request =>
  (for{
    product <- fetchProductAsync
    other    <- otherInfoAsync
  } yield {
    updateProductAsync().map{ writeResult => 
       asyncOperate1.map{ writeResult1 =>
       	 Redirect(routes.ProductController.editProduct(_id))
       }
    }    
  }).flatMap(f1 => f1.flatMap(f2 => f2))    
}

注意,for ... yield需要用一对圆括号括起来。转载请注明作者: joymufeng

转载于:https://my.oschina.net/joymufeng/blog/864056

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值