Promise简单实现

Promise

Promise 创建时实例化action实例, filfull, reject 两个闭包是action的两个入参,主要是负责succ和fail的处理,action是把filfull, reject合并后,通过then函数处理。

then函数是Promise内部函数,可以action中filfull,reject中的值处理后,再通过Promise包装返回,形成一个调用链。

    // then 处理
    func then(exec: @escaping (T) -> Promise?) -> Promise {

        return Promise{ [unowned self]  (resolve, reject) in


            let c1:fulfilled = { v in
                
                exec(v)?.doExec(callback: { (obj) in
                    resolve(obj)
                })
            }
            
            let c2:rejected = { e in
                
                reject(e)
            }
            
            self.executor(c1, c2)
        }
    }

处理Promise结果时,可以通过then获取上次Promise对象的action中携带的值,然后实例化 fulfilledCallback,rejectedCallback接受返回成功值,后者异常值

 // 处理fail
    func doCatch(callback: @escaping rejected) {
        
        rejectedCallback = callback
        
        let c1:fulfilled = { _ in }
        
        let c2:rejected = { [unowned self]  err in
            
            self.reject(err: err)
        }
        
        executor(c1, c2)
    }

完整实现

class Promise {
    
    typealias fulfilled = (T) -> Void
    typealias rejected = (Error) -> Void
    typealias action = (@escaping fulfilled,@escaping rejected) -> Void
    
    // 实例化执行器
    private let executor:action
    
    // 成功回调
    private var fulfilledCallback:fulfilled?
    // 异常回调
    private var rejectedCallback:rejected?
    
    
    init(exec:@escaping action) {
        executor = exec
    }
    
    // 处理成功结果
    private func resolve(result: T) {
        
        if fulfilledCallback == nil {
            return
        }
        fulfilledCallback!(result)
    }
    
    // 处理异常结果
    private func reject(err:Error) {
        
        if rejectedCallback == nil {
            return
        }
        rejectedCallback!(err)
    }
    
    // 处理succ
    func doExec(callback:@escaping fulfilled) {
        
        fulfilledCallback = callback

        let c1:fulfilled = { [unowned self] v in
            
            self.resolve(result: v)
        }
        
        let c2:rejected = { _ in  }
        
        executor(c1, c2) 
    }
    
    // 处理fail
    func doCatch(callback: @escaping rejected) {
        
        rejectedCallback = callback
        
        let c1:fulfilled = { _ in }
        
        let c2:rejected = { [unowned self]  err in
            
            self.reject(err: err)
        }
        
        executor(c1, c2)
    }
    
    // then 处理
    func then(exec: @escaping (T) -> Promise?) -> Promise {


        return Promise{ [unowned self]  (resolve, reject) in


            let c1:fulfilled = { v in
                
                exec(v)?.doExec(callback: { (obj) in
                    resolve(obj)
                })
            }
            
            let c2:rejected = { e in
                
                reject(e)
            }
            
            self.executor(c1, c2)

        }
    }
}

测试

        // fun1
        func getStr(pwd:String? = nil) -> Promise {
            
            // exec pwd
            let v = Promise.init { (fulfill, reject) in
                
                let v1 = Int(arc4random())%100 > 10 ? true : false
                
                if v1 {
                    
                    fulfill("true")
                } else {
                    
                    reject(MyErr.net)
                }
            }

            return v
        }
        
        // fun2
        func getInt(value:String? = nil) -> Promise {
            
            // exec value
            let v = Promise.init { (fulfill, reject) in
                
                fulfill(0)
            }
            
            return v
        }

        
        getStr().then { (v) -> Promise? in
            
            return getInt(value: v)
        }.doExec { (v) in
            
            print(v)
        }

你可能感兴趣的:(Promise简单实现)