Skip to content

Instantly share code, notes, and snippets.

@zgordon
Created June 27, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgordon/d58060d8d9db026ef8b70b15d5406670 to your computer and use it in GitHub Desktop.
Save zgordon/d58060d8d9db026ef8b70b15d5406670 to your computer and use it in GitHub Desktop.
Attempting to use async and await with Node WP API Client
// What I'm calling in my router
await Page.render( slug );
Gallery.render();
// The Page.render() code
export default class Page {
/**
* render - Display pages on the Page
*
* @param {String} slug The slug of the page to display
*/
static render( slug ) {
return new Promise( resolve => {
config.wp.pages()
.slug( slug )
.embed()
.perPage( 1 )
.then( pages => {
if ( pages[ 0 ] ) {
Helpers.renderContent( pages[ 0 ] );
} else {
Helpers.renderContent( config.page404 );
}
resolve();
} )
.catch( err => {
console.log( `Error: ${err}` );
} );
} );
}
}
@zgordon
Copy link
Author

zgordon commented Jun 27, 2017

The above code works, but I am wondering if the return new Promise is necessary since .pages() is already a promise. Couldn't seem to get it working without the extra Promise wrapper. Was wondering if you had any advice on how to clean up?

@kadamwhite
Copy link

This is interesting, and may be related to the fact that .then() is a method that we define manually, and not an actual promise method itself -- I'd try putting a .get() before the .then(), to start, and if that doesn't work maybe there's a workaround. I'll look into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment