I have a function called chaining
which chains multiple promises
and I want to call that function multiple times. For that, I am using a for
loop and I want that with index 0
, the chaining
function should be executed with index 0. (I have an array of properties ListingArray[]
and I want to use ListingArray[0] in one iteration of loop, ListingArray[1] in other iteration and so on).
Here is my chaining
function:
func chaining() {
firstly {
Promise_getIDOfOwnerFromCollection()
}.then { (IdsInCollection)-> Promise<[String]> in
return self.Promise_getOwnerListings(IDs: IdsInCollection)
}.then { (ownerListings) ->Promise<Void> in
return self.Promise_getReviews(ListingIDs: ownerListings)
}.done { (arg0) in
let () = arg0
print("Work Done")
}.catch { (error) in
print("Error is \(error.localizedDescription)")
}
}
And I am calling that function in loop like this.
for Count in 0...4 {
chaining()
}
Now the problem is that the function inside firstly
is instantly called 5 times before then
is executed. And I want the sequence to be like with Count
0, chaining
function should execute one time and then with Count
1, function should execute again.
The behaviour happening in your code is completely expected. Given you're instantiating the chaining 4 times, therefore the firstly job will be executed such number of times.
Instead you will need somehow provide a single instance of work for the firstly.
Currently: execute N times { firstly + rest of the job }
Expected: firstly + execute N times { rest of the job }
Here a code example based on yours.