Instantly share code, notes, and snippets.

@Shelob9 /1.js
Last active Nov 1, 2018

Embed
What would you like to do?
/**
* You may only use "await" inside of an asynchronous closure
*/
async () => {
//You may use await
};
/**
* Get the most recent post from CalderaForms.com
*/
async function getMostRecentPost(){
//Get posts and await results.
const posts = await fetch( 'https://calderaforms.com/wp-json/wp/v2/posts' );
//Once posts are returned, get the first one.
const post = posts[0];
return post;
};
/**
* Inside of an asynchronous closure, you can await results of your asynchronous function
*/
async () => {
const post = await getMostRecentPost();
console.log(post);
};
/**
* Get the most recent post from a WordPress site
*/
async function getMostRecentPost(postRoute) {
//Get posts and await results.
const posts = await fetch( postRoute );
//Once posts are returned, get the first one.
const post = posts[0];
return post;
};
/**
* Get the most recent posts from JoshPress.net and CalderaForms.com
*/
const getPosts = async () => {
//Get one post and await the result
const postOne = await getMostRecentPost('https://calderaforms.com/wp-json/wp/v2/posts');
//Get another post after the first post is returned.
const postTwo = await getMostRecentPost('https://joshpress.net/wp-json/wp/v2/posts');
//return both after second request is completed.
return [postOne,postTwo];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment