Skip to content

Web Development

kitiya edited this page Mar 8, 2020 · 16 revisions

HTTP

HTTP Request

  • GET
  • POST
  • PUT
  • DELETE

HTTPS Response

  • 1xx: Information
  • 2xx: Success
  • 3xx: Redirect
  • 4xx: Client errors
  • 5xx: Server errors

JSON

  • JSON.parse(jsonString)
  • JSON.stringnify(jsonObject)

AJAX

componentDidMount() {
  fetch(url) //return a promise (response)
    .then(response => response.json()) //return a promise (users)
    .then(users => { this.setState({myUsers: users}) });
}

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.

A promise constructor

let promise = new Promise((resolve, reject) => {
  // the function is executed automatically when the promise is constructed

  // after 1 second signal that the job is done with the result "done"
  setTimeout(() => resolve("done"), 1000);
}

Async Await

Simple example

// using promise
fetch("https://jsonplaceholder.typicode.com/users")
.then(response => response.json())
.then(console.log);

// using async/await
async function fetchData() {
  const response = await fetch("https://jsonplaceholder.typicode.com/users");
  const data = await response.json();
  console.log(data);
}

fetchData();

API

Clone this wiki locally