I have this Axios Async Await code in Nuxt.js, I am not sure on how and where to put the Promise.all here. I am trying to promise the getThemes() and getData(). Could somebody help me with the Promise.all code?
And do I have to put the Promise.all in the mounted()?
mounted() { this.getData(this.$route.params.id); this.getThemes(); }, methods: { async getThemes() { this.loading = true; await axios.get(`${process.env.API_URL}/v1/communication/email-themes`, {}).then((response) => { this.theme = response.data.data; this.selected = this.theme.filter(t => this.themeId === t.id)[0].id; this.loading = false; }).catch((error) => { this.loading = false; this.errormsg = error.response.data.message; }); }, async getData(id) { this.loading = true; await axios .get(`${process.env.API_URL}/v1/communication/email-templates/${id}`) .then(({ data }) => { this.templateName = data.data.name; this.templateCode = data.data.content; this.themeId = data.data.theme_id; this.loading = false; }).catch((error) => { this.loading = false; this.errormsg = error.response.data.message; }); }, async patchData(id) { await axios.put(`${process.env.API_URL}/v1/communication/email-templates/${this.$route.params.id}`, { name: this.templateName, content: this.templateCode, theme_id: this.selected }).then((response) => { this.results = response.data; this.loading = false; }).catch((error) => { this.loading = false; this.errormsg = error.response.data.message; }); } }
thisongetThemes()andgetData(), instead, just use the return value of your promises insidemounted(). Also, you are usingawaitpoorly, you shouldn't be usingthennorcatchbut atry catchblock.getThemes()' andgetData()` fail, one of them will replace theerrormsgfrom the other. You can catch both errors and concatenate them once you usePromise.all.then. In order to catch errors, you should implement one or more `try catch' blocks.getPromise()with your axios calls:getPromise(1)would becomeaxios.get(`${process.env.API_URL}/v1/communication/email-themes`, {}).