Skip to content
  • Should be map for the second one, and no need to specify return types for one-liners:

    firstly {
        session.dataTask(.promise, with: url)
    }.map(on: queue) { data, response in
        try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
    }.compactMap(on: queue) { imageDict in
        self.factory.getImage(fromDict: imageDict)
    }
    Edited by Max Howell
  • Author Owner

    You're right about the return types, but I wanted to point out in the examples that the data is being pushed down the chain.

    I suppose since we're using the 'try' it will either throw or create the dict, so it can be a .map. I just followed the original example set by: https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md

  • Oh PMK docs are wrong too then, whoops, will fix.

    Showing return types is fine if that is your goal and your justification is good.

  • Oh my bad compactMap is correct here, didn’t see the as?

  • Author Owner

    Cool, appreciate the input :) If it wasn't cast into an optional then .map would do just fine :) It would actually be clearer in the intent.

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment