PromiseKit 后退一步

PromiseKit go back one step

我的应用程序中有一系列 UIViewController(弹出窗口),用于从最终用户那里收集信息。我设法用这样的承诺将它们链接起来:

firstly {
        return showFirstPopup()
    }
    .then { info1 -> Promise<Info2> in
        dismissFirstPopup()
        //Do sth with info1...
        return showSecondPopup()
    }
    .then { info2 -> Promise<Info3> in
        dismissSecondPopup()
        //Do sth with info2...
        return showThirdPopup()
    }
    .then { info3 -> Promise<Info4> in
        dismissThirdPopup()
        //Do sth with info3...
        return showForthPopup()
    }
    ...
    .catch { error in
        //Handle any error or cancellation...
     }

例如,如果用户在第三个弹出窗口中按下返回键,我需要返回到上一个 "then" 而不是取消整个流程。

基本上我需要能够返回一步,让用户编辑数据,然后继续流程。

有没有办法用 PromiseKit 做到这一点?

以下是我对 PromiseKit 的发现:

PromiseKit has the concept of cancellation baked in. If the user cancels something then typically you don’t want to continue a promise chain, but you don’t want to show an error message either. So what is cancellation? It’s not success and it’s not failure, but it should handle more like an error, ie. it should skip all the subsequent then handlers. PromiseKit embodies cancellation as a special kind of error.

据此,您将跳过所有后续的 then 处理程序。 这是我找到此信息的 link:Promise Kit

这就是我最终使用的。希望这也能帮助其他人。

func myFunc(info: Info) -> Promise<()>
{
    return Promise { fulfill, reject in

        let firstPromise = shouldShowFirstPopup ? showFirstPopup() : Promise(value: info.info1)

        firstPromise
        .then { info1 -> Promise<Info2> in
            info.info1 = info1

            if (info.shouldShowSecondPopup) {
                return showSecondPopup()
            }

            return Promise(value: info.info2)
        }
        .then { info2 -> Promise<Info3> in
            info.info2 = info2
            info.shouldShowSecondPopup = false

            if (info.shouldShowThirdPopup) {
                return showThirdPopup()
            }

            return Promise(value: info.info3)
        }
        .then { info3 -> Promise<()> in
            info.info3 = info3
            info.shouldShowThirdPopup = false

            return processEverything()
        }
        .then { _ -> () in
            fulfill(())
        }
        .catch { error in
            switch error {
                case MyErrors.backPressed(let popupType):
                    switch popupType {
                        case .firstPopup:
                        reject(MyErrors.userCanceled)
                        return

                        case .secondPopup:
                        info.shouldShowFirstPopup = true
                        info.shouldShowSecondPopup = true

                        case .thirdPopup:
                        info.shouldShowSecondPopup = true
                        info.shouldShowThirdPopup = true

                        default:
                        reject(MyErrors.defaultError(message: "Not implemented case exception"))
                        return
                    }

                    firstly {
                        return myFunc(info: info)
                    }
                    .then { _ -> () in
                        fulfill(())
                    }
                    .catch { error2 in
                        reject(error2)
                    }

                default:
                reject(error)
            }
        }
    }
}