I've got a vue.js application in which I need to await a call to the Prismic API. The problem is the call starts from the created hook, but I don't know how to tell vue to await the hook.
The code is like this:
...
async created() {
await this.getContent();
},
methods : {
async getContent () {
let document = null;
try {
document = await this.$prismic.client.getSingle(...);
} catch(err) {
return;
}
// add content to meta tags
},
....
As you can see, I'm awaiting the call to this.$prismic.client.getSingle(...) before setting the content in the meta tags (which works fine), but because this requires the getContent() method to be async, I need to also await the call to this.getContent() in the created() hook... which means I need to make the created() hook async. At this point, I'm at the mercy of Vue. I don't know how to tell it to await the call to created().
This is important because we're trying to get the google search crawler to read our meta tags. We're getting inconsistent results. In the Google Search Console, it sometimes finds the tags, sometimes not. We're trying to see if an async/await structure to our code will help. That way, the crawler will wait for the javascript to finish executing before looking for the tags (or so we expect). But if we can't await the call to created(), there's no point in awaiting the other method calls.
Is there a way to deal with this problem? Thanks.
EDIT: The question at Is `async/await` available in Vue.js `mounted`? is exactly the same as mine but the solution doesn't apply. The solution there is to control the rendering of a component with v-if="flag" where flag is set at the end of the async method. In my case, I'm at the mercy of the google crawler which I can't control.