-
Notifications
You must be signed in to change notification settings - Fork 0
Web Development
kitiya edited this page Mar 9, 2020
·
16 revisions
GETPOSTPUTDELETE
- 1xx: Information
- 2xx: Success
- 3xx: Redirect
- 4xx: Client errors
- 5xx: Server errors
JSON.parse(jsonString)JSON.stringnify(jsonObject)
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.
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);
}// 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();