Example using a Promise
var getPosts = () => {
fetch('/wp-json/wp/v2/posts')
.then((response) => response.json())
.then((json) => { console.log(json); });
};
Example using async / await
Note: await
only works when inside an async
function.
var getPosts = async () => {
var response = await fetch('/wp-json/wp/v2/posts');
var json = await response.json()
console.log(json);
};
Leave a Reply