Last active
September 8, 2022 10:47
-
-
Save amorkovin/53a13adccea121643470c96e583c0713 to your computer and use it in GitHub Desktop.
REST API WordPress JavaScript fetch await async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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