How should I chain multiple Promises in PromiseKit 6

扶醉桌前 提交于 2021-01-07 06:26:55

问题


I am having a hard time using PromiseKit 6. I have several asynchronous functions which need to run one after the other so I need to chain them. I was wondering what's the correct way to use them. Also, no help from Here. So, as in PromiseKit6, you have to return a Promise from then, which is what is specifically giving me headache.

I have an example:

First Function

func A()->Promise<[String]> {
  return Promise { seal in 
    //Do some Asynch operation and got String Array , `temp_Array`
        seal.fullfill(temp_Array)
      }
    }

Second Function

func B() -> Promise<[String]> {
 return Promise { seal in 
    //Do some Asynch operation and got String Array , `temp2_Array`
        seal.fullfill(tempArray)
      }
    }

Third Function

func C() -> Promise<Void> {
  return Promise { seal in 
    //Do some Asynch operation and got String Array , `tempArray`
        seal.fullfill_()
      }
    }
 

Now I want to call all three Functions in Chain like below

    firstly {
               self.A()
    }.then { (Array_Receieved_From_A_When_Completed ) ->Promise<[String]> // Confused with this return

             //Do some operation with `Array_Receieved_From_A_When_Completed `
             self.B() /* How to call that function */              
    
    }.then {(Array_Receieved_From_B_When_Completed ) -> Promise<Void> {
           
          //Do some operation with `Array_Receieved_From_B_When_Completed`
          self.C()   /* How to call that function */

   }.done {() in {
    
   }.catch(error) in {

       print(error)
   }

How should I manage this? How data from one then to other then goes? I was fed up with handling asynchronous functions and thought of using PromiseKit but this is also giving a tough time.

来源:https://stackoverflow.com/questions/62780222/how-should-i-chain-multiple-promises-in-promisekit-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!