From 0a7017f9735c64332d60d0e3381f2d2b602aef87 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 2 Nov 2015 18:09:15 +0000 Subject: [PATCH] update website --- docs/network.html | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/network.html b/docs/network.html index 198c3278067..f32814b6542 100644 --- a/docs/network.html +++ b/docs/network.html @@ -12,14 +12,31 @@ request.onreadystatechange }; request.open('GET', 'https://mywebsite.com/endpoint.php'); -request.send();

Please follow the MDN Documentation for a complete description of the API.

As a developer, you're probably not going to use XMLHttpRequest directly as its API is very tedious to work with. But the fact that it is implemented and compatible with the browser API gives you the ability to use third-party libraries such as Parse or super-agent directly from npm.

Fetch #

fetch 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://mywebsite.com/endpoint.php') +request.send();

Please follow the MDN Documentation for a complete description of the API.

As a developer, you're probably not going to use XMLHttpRequest directly as its API is very tedious to work with. But the fact that it is implemented and compatible with the browser API gives you the ability to use third-party libraries such as Parse or super-agent directly from npm.

Fetch #

fetch is a better networking API being worked on by the standards committee and is already available in Chrome. It is available in React Native by default.

Usage #

fetch('https://mywebsite.com/endpoint/')

Include a request object as the optional second argument to customize the HTTP request:

fetch('https://mywebsite.com/endpoint/', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + firstParam: 'yourValue', + secondParam: 'yourOtherValue', + }) +})

Async #

fetch returns a Promise that can be processed in two ways:

  1. Using then and catch in synchronous code:
fetch('https://mywebsite.com/endpoint.php') .then((response) => response.text()) .then((responseText) => { console.log(responseText); }) .catch((error) => { console.warn(error); - });

WebSocket #

WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.

var ws = new WebSocket('ws://host.com/path'); + });
  1. Called within an asynchronous function using ES7 async/await syntax:
async getUsersFromApi() { + try { + let response = await fetch('https://mywebsite.com/endpoint/'); + return response.users; + } catch(error) { + throw error; + } +}

WebSocket #

WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.

var ws = new WebSocket('ws://host.com/path'); ws.on('open', function() { // connection opened