收集成功的承诺 [英] Collect array of successful promises

查看:103
本文介绍了收集成功的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中使用PromiseKit 3.0,并且有一个promises [Promise<Int>]数组.我想将所有成功的诺言汇集成一个诺言. Promise<[Int]>.

whenjoin均被拒绝,即使其中一个包含的promise也被拒绝.根据文档,我应该能够使用join,并且该错误将包含一个实现值的数组,但是在Swift中,该错误包含所有传入的promise,而不是实现值.

任何帮助将不胜感激.

解决方案

我现在看到我需要一个新功能:

https://gist.github.com/dtartaglia/2b19e59beaf480535596

/**
Waits on all provided promises.

`any` waits on all provided promises, it rejects only if all of the promises rejected, otherwise it fulfills with values from the fulfilled promises.

- Returns: A new promise that resolves once all the provided promises resolve.
*/
public func any<T>(promises: [Promise<T>]) -> Promise<[T]> {
    guard !promises.isEmpty else { return Promise<[T]>([]) }
    return Promise<[T]> { fulfill, reject in
        var values = [T]()
        var countdown = promises.count
        for promise in promises {
            promise.then { value in
                values.append(value)
            }
            .always {
                --countdown
                if countdown == 0 {
                    if values.isEmpty {
                        reject(AnyError.Any)
                    }
                    else {
                        fulfill(values)
                    }
                }
            }
        }
    }
}

public enum AnyError: ErrorType {
    case Any
}

I'm using PromiseKit 3.0 in Swift and I have an array of promises [Promise<Int>]. I want to gather up all the promises that succeed into a single promise. Promise<[Int]>.

Both when and join reject if even one contained promise rejects. According to the docs, I'm supposed to be able to use join and the error will contain an array of the fulfilled values, but in Swift the error contains all the promises that were passed in, not the fulfilled values.

Any help would be appreciated.

解决方案

I see now I need a new function:

https://gist.github.com/dtartaglia/2b19e59beaf480535596

/**
Waits on all provided promises.

`any` waits on all provided promises, it rejects only if all of the promises rejected, otherwise it fulfills with values from the fulfilled promises.

- Returns: A new promise that resolves once all the provided promises resolve.
*/
public func any<T>(promises: [Promise<T>]) -> Promise<[T]> {
    guard !promises.isEmpty else { return Promise<[T]>([]) }
    return Promise<[T]> { fulfill, reject in
        var values = [T]()
        var countdown = promises.count
        for promise in promises {
            promise.then { value in
                values.append(value)
            }
            .always {
                --countdown
                if countdown == 0 {
                    if values.isEmpty {
                        reject(AnyError.Any)
                    }
                    else {
                        fulfill(values)
                    }
                }
            }
        }
    }
}

public enum AnyError: ErrorType {
    case Any
}

这篇关于收集成功的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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