Skip to content

Instantly share code, notes, and snippets.

@amorkovin
Last active September 8, 2022 10:47
Show Gist options
  • Save amorkovin/53a13adccea121643470c96e583c0713 to your computer and use it in GitHub Desktop.
Save amorkovin/53a13adccea121643470c96e583c0713 to your computer and use it in GitHub Desktop.
REST API WordPress JavaScript fetch await async
const urlRequest = 'http://wp.loc/wp-json/wp/v2/posts?_fields=title';
async function getPostsFromWp() {
try {
const response = await fetch( urlRequest )
const data = await response.json()
return data
} catch ( e ) {
console.error( e )
}
}
const resultFromWP = getPostsFromWp();
const urlRequest = 'http://nwp.loc/wp-json/wp/v2/posts?_fields=title';
async function getPostsFromWp() {
try {
const response = await fetch( urlRequest )
const data = await response.json()
return data
} catch ( e ) {
console.error( e )
}
}
const resultFromWP = getPostsFromWp();
resultFromWP.then( data => console.log( data ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment