google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.8k stars 294 forks source link

Return a promise in `then` of a `all` promise #118

Closed anhtukhtn closed 5 years ago

anhtukhtn commented 5 years ago

Firstly, thank for the great repo.

I try to (way 1) // it works in JS

func fetchInfo() -> Promise<[String]> {
    return  all(fetch1(),
                fetch2())
                .then { [weak self] in
                    self?.doThing()
                    return($0)       // got 'Unexpected non-void return value in function' here
                }
}

I have to do (way 2)

func fetchInfo() -> Promise<[String]> {
    return Promise<[String]> { [weak self] fulfill, _ in  
        all(fetch1(),
                fetch2())
                .then { [weak self] in
                    self?.doThing()
                   fulfill($0)
                }
    }
}

The question is can I archive it by the way 1?

Thank you very much

shoumikhin commented 5 years ago

Are you effectively trying to do something like this?

func fetchInfo() -> Promise<[String]> {
  return all(fetch1(), fetch2()).then { [weak self] (fetch1Data, _) -> [String] in
    self?.doThing()
    return fetch1Data
  }
}
anhtukhtn commented 5 years ago

Thank you very much. It works perfectly..