Swift & PromiseKit: resolving ALL promises from a loop

I am looking for a solution in Swift3 to simultaneously resolve a dynamic number of promises, for example. like this sample in javascript:

var promises = []; for(var i = 0; i < 5; i++) { var promise = $http.get('/data' + i); promises.push(promise); } $q.all(promises).then(doSomethingAfterAllRequests); 

https://daveceddia.com/waiting-for-promises-in-a-loop/

There was a call to the Craft library for Swift2 that could do this ( https://github.com/supertommy/craft ), but it is no longer supported.

Does anyone know if I can do this using PromiseKit or another library?

thanks bunch!

+5
source share
1 answer

You can see when , which can provide what you need and will be covered here .

Use a loop to put your promises in an array, and then do something like this:

 when(fulfilled: promiseArray).then { results in // Do something }.catch { error in // Handle error } 
+6
source

Source: https://habr.com/ru/post/1267323/


All Articles