Created
June 27, 2017 08:09
-
-
Save zgordon/d58060d8d9db026ef8b70b15d5406670 to your computer and use it in GitHub Desktop.
Attempting to use async and await with Node WP API Client
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
// 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}` ); | |
} ); | |
} ); | |
} | |
} |
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
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?