GithubHelp home page GithubHelp logo

Comments (7)

khanlou avatar khanlou commented on August 22, 2024

It isn't clear from the readme, but it's an extension I added onto URLSession that sends the request and returns a promise. It works almost exactly the same as the promise above it in the readme. What do you propose for fixing the lack of clarity?

from promise.

SintraWorks avatar SintraWorks commented on August 22, 2024

That explains it. I had a feeling you did not simply make a mistake, but I couldn't add things up...

Anyway, for making it clear what is going on, you could mention that you're using the extension just before the first code example that uses it. (If your extension is on GitHub, maybe add a link to it.)

And while I'm at it: many thanks for your excellent blog posts!

from promise.

SintraWorks avatar SintraWorks commented on August 22, 2024

I made it an excercise to recreate your URLSession extension. Any comments on the result?

extension URLSession {
    open func dataTask(with request: URLRequest) -> Promise<(Data, HTTPURLResponse)> {
        return Promise<(Data, HTTPURLResponse)>(work: { fulfill, reject in
           self.dataTask(with: request, completionHandler: { data, response, error in
                if let error = error {
                    reject(error)
                } else if let data = data, let response = response as? HTTPURLResponse {
                    fulfill((data, response))
                } else {
                    fatalError("Something has gone horribly wrong.")
                }
            }).resume()
        })
    }
}

from promise.

khanlou avatar khanlou commented on August 22, 2024

Nailed it! The only thing I would change is returning the promise directly instead of creating an intermediate variable and returning that.

from promise.

SintraWorks avatar SintraWorks commented on August 22, 2024

Thanks, getting rid of the intermediate makes sense, of course.

As an update on "fixing the lack of clarity": you could show the extension in the Creating Promises paragraph, right where you show how to wrap URLSession. Rather than assigning the promise to a variable, show the extension code. You wouldn't even need to adjust the the paragraph text around it, since the extension code exemplifies the idea of "wrapping" an API even more clearly than the current example.

from promise.

kelan avatar kelan commented on August 22, 2024

@SintraWorks: Minor correction in that code listing:
I think you should use self instead of URLSession.shared in this line:

            URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in

so that you make a task in the session that receives the method call, instead of always in the shared session.

from promise.

SintraWorks avatar SintraWorks commented on August 22, 2024

@kelan: Thanks for noticing that. You are right, of course.

from promise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.