斯威夫特& PromiseKit:解决循环中的所有承诺 [英] Swift & PromiseKit: Resolving ALL promises from a loop

查看:101
本文介绍了斯威夫特& PromiseKit:解决循环中的所有承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找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?

打一堆!

推荐答案

您可以查看when,它可以提供您所需要的内容,并在

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
}

这篇关于斯威夫特&amp; PromiseKit:解决循环中的所有承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆