-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extend Network/Fetch API documentation #3552
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
cc @brentvatne |
I also noticed in the course of working with |
In RN we include regenerator by default so try-catch should work out of the box. Might be nicer to show that instead of then/catch. |
@@ -37,7 +37,7 @@ As a developer, you're probably not going to use XMLHttpRequest directly as its | |||
|
|||
## Fetch | |||
|
|||
[fetch](https://fetch.spec.whatwg.org/) is a better networking API being worked on by the standard committee and is already available in Chrome. It is available in React Native by default. | |||
[fetch](https://fetch.spec.whatwg.org/) is a better networking API being worked on by the standard committee and is already available in Chrome. It is available in React Native by default. Fetch returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be standards committee rather than standard committee
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also: fetch
returns a... rather than Fetch returns a..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted and fixed
Yeah I agree we should include this -- it would be ideal to have it as an alternative. @whokilledtheelectricmonk - are you familiar with async/await enough to show your example with that syntax? If not that's fine, I can merge this and add it. Thank you! |
Only problem with this is that you can only use it in functions declared as Thoughts? EDIT: I just added an example - let me know what you think |
@whokilledtheelectricmonk updated the pull request. |
2 similar comments
@whokilledtheelectricmonk updated the pull request. |
@whokilledtheelectricmonk updated the pull request. |
async getUsersFromApi() { | ||
try { | ||
let response = await fetch('https://mywebsite.com/endpoint/'); | ||
return await response.users; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the await
@whokilledtheelectricmonk updated the pull request. |
@@ -50,6 +77,22 @@ fetch('https://mywebsite.com/endpoint.php') | |||
}); | |||
``` | |||
|
|||
2. Called within an asynchronous function using ES7 `async`/`await` syntax: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this transform enabled by default both on iOS and Android? If not people will find this example confusing because it won't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's purely implemented in JS. (The iOS bridge has support for defining bridged async functions, which we need to add to Android. But you can use pure JS async functions on both platforms.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, just wasn't sure whether ES7 was enabled in the Babel config by default.
Just found it :)
https://github.com/facebook/react-native/blob/master/packager/react-packager/.babelrc#L18
I'll let @brentvatne do the final review as he's been reading through this before. Thanks for adding it! |
(See my inline comment about ES7) |
Should I squash these commits or rebase? |
Yes, if you could please squash documentation-related commits that'd be great and help keep the commit history clean. |
- Mention that Fetch returns a Promise (makes example with .then clearer to newcomers to ES6/ES7) - Add example of Fetch usage with second argument (helps clarify how e.g., a POST request to an API might be constructed) - Add async/await example
@ide @brentvatne do you want to merge this in for 0.15? It has been squashed. |
extend Network/Fetch API documentation
fyi: Commits to the docs go out right away regardless of the branch cutoff (this might change when we have versioned docs, but right now the docs are for master). |
(makes example with .then clearer to newcomers to ES6/ES7)
(helps clarify how e.g., a POST request to an API might be constructed)