斯威夫特& PromiseKit:解决循环中的所有承诺 [英] Swift & PromiseKit: Resolving ALL promises from a loop
问题描述
我正在寻找Swift3中的一种解决方案,以一次解决动态数量的Promise,例如就像JavaScript中的这个示例一样:
I'm looking for a solution in Swift3 to resolve a dynamic number of promises all at once, e.g. 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/
Swift2有一个库调用"Craft",可以做到这一点( https://github.com/supertommy/craft ),但不再维护.
There was a library call 'Craft' for Swift2 that could do that (https://github.com/supertommy/craft), but it's no longer maintained.
有人知道我是否可以或如何使用PromiseKit或其他库来做到这一点?
Does anyone know if or how I could do that with PromiseKit or another library?
打一堆!
推荐答案
You can look into when
which may provide what you need and is covered here.
使用循环将您的Promise放入数组中,然后执行以下操作:
Use the loop to put your promises into an array and then do something like this:
when(fulfilled: promiseArray).then { results in
// Do something
}.catch { error in
// Handle error
}
这篇关于斯威夫特& PromiseKit:解决循环中的所有承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!