首页 > 解决方案 > 向数组添加 Promise 并防止它们自动解析

问题描述

我希望将任意数量的 Promise 添加到数组中,并让它们在某个时候一个接一个地依次解决。快速伪 swift-code 将是(不沉迷于如何顺序调用承诺):

var someArray: [Promise<Bool>] = [first, second, third, fourth, ...]
...
someArray.append(contentsOf: getMorePromises())

...

firstly {
   ...
}.then { 
  // use someArray here and compose a single Promise 
  // that links to the next element in the array using .then
}

我遇到的问题是,someArray甚至在我到达firstly. 如何防止这种情况发生,以便我在数组中保留承诺,并且只有在其中一个then部分内时才让它们解决?

标签: iosswiftpromisekit

解决方案


我已经想通了。在将它们添加到数组之前将它们包装在闭包中,然后在将它们拉出数组以使其触发时解开闭包。


推荐阅读