将[Future <Object,Error>]转换为Future <[Object],Error>

时间:2019-12-04 01:28:09

标签: swift parallel-processing functional-programming future

有什么方法可以解析下面的代码,并将其值作为数组Future <[Object],Error>返回。我正在使用BrightFutures未来的实现方式

return apiService.getArrayObject()
   .flatMap(NetworkQueue.context) { (arrayObjects: [ArrayObject]) -> Future<[Object], Error> in
      let objects = arrayObjects.map {
         apiService.getObject(of: $0.objectId)
      }
      return objects // ERROR since objects is [Future<Object,Error>] rather than needed output
    }

1 个答案:

答案 0 :(得分:0)

使用顺序

   return apiService.getArrayObject()
      .flatMap(NetworkQueue.context) { (arrayObjects: [ArrayObject]) -> Future<[Object], Error> in
          let objects: [Future<Object, Error>] = arrayObjects.map {
              apiService.getObject(of: $0.objectId)
          }
          let sequenceFuture: Future<[Object], NoError> = objects
                    .sequence()

         return sequenceFuture
    }