Swift 2 Promisekit 3链接asyc方法疯狂

时间:2016-01-22 19:20:31

标签: swift2 promisekit

我有3个承诺,每个承诺单独工作:

DataManager.reverseGeoCodePromise("42.527328", longitude: "-83.146928").then { data in
    print("Zip \(data)")
}

DataManager.getEnviroUVWithZipPromise("48073").then { data in
    print("UV \(data.getUVIndex())")
}

DataManager.getCurrentForZipPromise("48073").then { data in
    print("AirNow \(data[0].Latitude)")
}

我一直在尝试关注(sparce)关于promisekit 3的快速文档(并且耗尽谷歌和堆栈溢出)。我尝试链接这些异步方法并不是完全同步 - 所有print(数据)都是null,但方法中的断点显示数据。

DataManager.reverseGeoCodePromise("42.527328", longitude: "-83.146928").then { data in
    print("Promise Zip \(data)")
}.then { data -> Void in
    DataManager.getEnviroUVWithZipPromise("48073")
    print("Promise Enviro \(data)")
}.then { data -> Void in
    DataManager.getCurrentForZipPromise("48073")
    print("Promise AirNow \(data)")
}.error { error in
    print("Promise doh!")
}

我希望第一次调用(zip)中的数据传递给后续调用,并能够处理最后一次调用后的所有调用中的数据。非常感谢任何见解!

1 个答案:

答案 0 :(得分:0)

您的问题是您的关闭正在返回Void而不是Promise<T>then块并不期望返回任何内容,因此它会继续,而不会等待承诺被履行或拒绝。

以下是您的完整示例。您可以将其粘贴到PromiseKit游乐场中进行测试。

var zipcode: String?

UIApplication.sharedApplication().networkActivityIndicatorVisible = true

firstly {

    return DataManager.reverseGeoCodePromise("42.527328", longitude: "-83.146928")

}.then { (data: String) -> Promise<String> in
    // ^ I expect to receive a string. I promise to return a string        

    zipcode = data
    // ^ Store zip code so it can be used further down the chain

    print("Returned from reverseGeoCodePromise: \(data)")
    return DataManager.getEnviroUVWithZipPromise(zipcode!)

}.then { (data: String) -> Promise<String> in
    // ^ I expect to receive a string. I promise to return a string

    print("Returned from getEnviroUVWithZipPromise: \(data)")
    return DataManager.getCurrentForZipPromise(zipcode!)

}.then { (data: String) -> Void in
    // ^ I expect to receive a string. I return nothing

    print("Returned from getCurrentForZipPromise: \(data)")

}.always {

    UIApplication.sharedApplication().networkActivityIndicatorVisible = false

}.error { error in

    switch error {
    case DataManagerError.FailedDueToX:
        print("got an error")
    default:
        print("unknown error")
    }

}

示例自定义错误类型:

enum DataManagerError: ErrorType {
    case FailedDueToX
}

示例数据管理器承诺:

class DataManager {
    static func reverseGeoCodePromise(lat: String, longitude: String) -> Promise<String> {
        return Promise { fulfill, reject in
            if 1 == 1 {
                fulfill("48073")
            } else {
                reject(DataManagerError.FailedDueToX)
            }
        }
    }

    static func getEnviroUVWithZipPromise(zip: String) -> Promise<String> {
        return Promise { fulfill, reject in
            if 1 == 1 {
                fulfill("Two")
            } else {
                reject(DataManagerError.FailedDueToX)
            }
        }
    }

    static func getCurrentForZipPromise(zip: String) -> Promise<String> {
        return Promise { fulfill, reject in
            if 1 == 1 {
                fulfill("Three")
            } else {
                reject(DataManagerError.FailedDueToX)
            }
        }
    }

}