带有可选承诺的 PromiseKit

PromiseKit with optional promises

我目前正在研究是否应该将 PromiseKit 集成到现有项目中。

我的主要问题是我需要实现一个最多可以调用 5 个 Web 服务的业务逻辑。其中一些是根据之前的结果调用的。

我当前的体系结构基于将代码分解为多个函数,这些函数具有相互调用的闭包。

我正在尝试找出是否可以使用 PromiseKit(或其他任何东西)编写更易于管理的代码。

这是我需要完成的一些伪代码:

        // if true, the phone validation is skipped
        let forceRequest = false
        // true if a 3rd party web-service has checked the phone number
        let isVerified = true
        // true if the 3rd party checked the phone number and it is valid
        var isValid = false

        if !isVerified {
            // update value from 3rd party web-service
            isValid = isValidPhoneNumberPromise()
        }

        // If the phone no is invalid stop execution (unless forced)
        if !isValid && !force {
            throw MyError.error1
        }

        // web request to create order
        createOrderPromise()

        // if we have a valid phone number, first send an SMS, then update log
        if isValid {
            sendSmsPromise()
            updateLogPromise()
        }

根据 totiG 的回答,我做出了以下变化:

var isValid = isValid

        firstly
        {
            return Controller.verify(isVerified: isVerified, isValid: isValid)
        }
        .then { _isValid -> Promise<Int> in
            isValid = _isValid
            return Controller.createOrder()
        }
        .then
        { _ -> Promise<Bool> in
            if isValid {
                return Controller.isSendSms()
            }

            return Promise (value: true)
        }
        .then
        { _ -> Promise<Bool> in
            if isValid {
                return Controller.updateLog()
            }

            return Promise (value: true)
        }
        .catch
        { error in
            print (error)
        }

是的,您可以使用 PromiseKit 来做到这一点。我已经写了一个基本的例子来展示你可能需要的东西。请记住,您会在步骤失败时抛出错误并在 catch 块中处理这些失败。在我的示例中,验证步骤通过,但如果调用 isValidPhoneNumber,它将停止 运行ning 的其他步骤。在我放置 Promise(value: ) 的地方,您可以放置​​实际的 Web 服务调用。如果更新日志的最后一步总是需要运行,你可以把它放在.always

enum Errors: Error {
    case invalidPhone
    case orderFailed
}

func orderPromise() {
    firstly {
        self.verify(isVerified: false, force: true)
    }.then { _ in
        self.createOrder()
    }.then { orderNumber in
        self.sendSms(orderNumber: orderNumber)
    }.then { smsSent in
        self.updateLog(smsSent: smsSent)
    }.catch { error in
        //Do something with the error
    }
}

private func verify(isVerified: Bool, force: Bool) -> Promise<Bool> {
    if isVerified || force {
        return Promise(value: true)
    }
    return isValidPhoneNumber()
}

private func isValidPhoneNumber() -> Promise<Bool> {
    return Promise(error: Errors.invalidPhone) //Assume that this fails, then catch in 'orderPromise' will be run
}

private func createOrder() -> Promise<String> {
    //Assume an order number is being passed back to use in the message
    return Promise(value: "Order100")
}

private func sendSms(orderNumber: String) -> Promise<Bool> {
    return Promise(value: true)
}

private func updateLog(smsSent: Bool) -> Promise<Bool> {
    return Promise(value: true)
}