iosswiftpromisekit

Adding promises to an array and preventing them from getting resolved automatically


I wish to add an arbitrary number of promises to an array and have them resolve sequentially one after another at some point. Quick pseudo swift-code would be (without indulging more into how the promises are called sequentially):

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
}

The issue I'm experiencing is that all the promises in someArray begin to resolve automatically (understandibly) even before I get to firstly. How can I prevent this such that I keep the promises around in the array and only have them resolve when inside of one of the then sections?


Solution

  • I’ve figured it out. Wrap the promises in closures before adding these to an array and then unwrap the closure when pulling these out of the array to have them fire.