-
-
Save Shelob9/252011dff617b0fb9239865c6a045a37 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* You may only use "await" inside of an asynchronous closure | |
*/ | |
async () => { | |
//You may use await | |
}; |
This file contains 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
/** | |
* 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; | |
}; |
This file contains 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
/** | |
* Inside of an asynchronous closure, you can await results of your asynchronous function | |
*/ | |
async () => { | |
const post = await getMostRecentPost(); | |
console.log(post); | |
}; |
This file contains 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
/** | |
* 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