Skip to content
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

Merged
merged 1 commit into from
Nov 2, 2015
Merged

Conversation

alexkrolick
Copy link
Contributor

  • 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)

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @sahrens, @mkonicek and @vjeux to be potential reviewers.

@facebook-github-bot
Copy link
Contributor

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!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 20, 2015
@vjeux
Copy link
Contributor

vjeux commented Oct 20, 2015

cc @brentvatne

@alexkrolick
Copy link
Contributor Author

I also noticed in the course of working with fetch that sending a stringified JSON as the request body (as per github/fetch#post-json) without setting the content-type header fails with a network error, at least for the endpoint I was using. I included that as the default in the example, but it might be worth mentioning explicitly.

@ide
Copy link
Contributor

ide commented Oct 20, 2015

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).
Copy link
Collaborator

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

Copy link
Collaborator

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..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted and fixed

@brentvatne
Copy link
Collaborator

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.

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!

@alexkrolick
Copy link
Contributor Author

Yeah I agree we should include this -- it would be ideal to have it as an alternative.

Only problem with this is that you can only use it in functions declared as async. Unless I'm mistaken, you can't define builtin React methods that way, so you can't use the try/catch/await syntax inside of, e.g., a componentDidMount method. I think the thing to do here is add a section in the docs about Promises and how they can be used with different syntax, and link to that when finished instead of the Mozilla page.

Thoughts?

EDIT: I just added an example - let me know what you think

@facebook-github-bot
Copy link
Contributor

@whokilledtheelectricmonk updated the pull request.

2 similar comments
@facebook-github-bot
Copy link
Contributor

@whokilledtheelectricmonk updated the pull request.

@facebook-github-bot
Copy link
Contributor

@whokilledtheelectricmonk updated the pull request.

async getUsersFromApi() {
try {
let response = await fetch('https://mywebsite.com/endpoint/');
return await response.users;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the await

@facebook-github-bot
Copy link
Contributor

@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:
Copy link
Contributor

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.

Copy link
Contributor

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.)

Copy link
Contributor

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

@mkonicek
Copy link
Contributor

I'll let @brentvatne do the final review as he's been reading through this before. Thanks for adding it!

@mkonicek
Copy link
Contributor

(See my inline comment about ES7)

@alexkrolick
Copy link
Contributor Author

Should I squash these commits or rebase?

@ide
Copy link
Contributor

ide commented Oct 27, 2015

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
@alexkrolick
Copy link
Contributor Author

@ide @brentvatne do you want to merge this in for 0.15? It has been squashed.

ide pushed a commit that referenced this pull request Nov 2, 2015
extend Network/Fetch API documentation
@ide ide merged commit a456ac3 into facebook:master Nov 2, 2015
@ide
Copy link
Contributor

ide commented Nov 2, 2015

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants