Comments (7)
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.
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.
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.
Nailed it! The only thing I would change is returning the promise directly instead of creating an intermediate variable and returning that.
from promise.
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.
@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.
@kelan: Thanks for noticing that. You are right, of course.
from promise.
Related Issues (20)
- Difficulty understanding tapping version of "then" HOT 1
- error: invalid target name at 'PromiseTests'; name of non-test targets cannot end in 'Tests' HOT 3
- TravisCI making it look like it works HOT 6
- Tests while in SPM HOT 10
- Promise chaining after `catch` is lacking HOT 2
- `then` methods type inferring issue HOT 13
- As of #43, the project might be incompatible with Xcode prior to 9.3 GM and Swift prior to 4.1 HOT 3
- Add Xcode 9.2 deployment in Travis CI.
- What about 2.0.2 ? HOT 2
- Static libs now support Swift - Promise should support static libs :) HOT 2
- Update Podspec HOT 6
- Callback threading problems HOT 3
- How to switch catches on error types? HOT 9
- Require only App-Extension-Safe API HOT 2
- No issue, just a THANK YOU HOT 1
- Suggestion: AllSettled implementation HOT 5
- Race condition when chaining a promise after being fulfilled? HOT 1
- Conflict with State enum and @State in SwiftUI HOT 3
- Not sure how to return Void promise HOT 3
- Catch Clause Not Invoked HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promise.