Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js how to get data fetch

// way 1
const getData = async (url) => {
  const response = await fetch(url)
  const json = await response.json()

  return json
}

const url = `https://jsonplaceholder.typicode.com/posts?userId=3`

try {
  const data = await getData(url)
  console.log(data)
} catch (error) {
  console.log(error.message)
}

/* ------------ Way 2 --------------- */
const getData = (url) =>
  new Promise((resolve, reject) => {
    fetch(url)
      .then(response => response.json())
      .then(json => resolve(json))
      .catch(error => reject(error))
  })

const url = `https://jsonplaceholder.typicode.com/todos?completed=true&userId=2`

getData(url)
  .then(data => console.log(data))
  .catch(error => console.log(error.message))
Comment

js undici fetch data

    import {fetch} from 'undici';

    async function fetchJson() {
        const res = await fetch('https://example.com')
        const json = await res.json()
        console.log(json);
    
Comment

javascript how to fetch data

.catch(function() {

});
Comment

PREVIOUS NEXT
Code Example
Javascript :: js toggle 
Javascript :: jquery get duration video tag 
Javascript :: kebab case javascript 
Javascript :: span change jquery 
Javascript :: react-native navigation screen props 
Javascript :: how to serve html node server 
Javascript :: convert currency to string javascript 
Javascript :: add class with javascript 
Javascript :: convert associative array to json javascript 
Javascript :: javascript tostring method 
Javascript :: jquery get dropdown list selected value 
Javascript :: copy to clipboard reatjs 
Javascript :: javascript getcontext 
Javascript :: how to get the size of the window in javascript 
Javascript :: cypress check element have attribute 
Javascript :: disabling submit button until all fields have values 
Javascript :: javascript edit form value 
Javascript :: how the filter() function works javascript 
Javascript :: what is normalize in javascript 
Javascript :: angular redirect to external url 
Javascript :: regex js pattern tags 
Javascript :: sanitizer content nodejs 
Javascript :: nodemailer, mailer, nodemailer npm 
Javascript :: No provider for ReducerManager 
Javascript :: javascript class methods 
Javascript :: js iso date split 
Javascript :: javascript MIN_VALUE 
Javascript :: react native safeareaview 
Javascript :: javascript return object property from array 
Javascript :: express receive post data 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =