I have this code and I want save the result of Promise.all into two variables. The problem is that I gets undefined when (this.data = data)
const {data,recents} : any = await Promise.all([
this.$store.dispatch('algo', input),
this.$store.dispatch('algo', input)
])
this.data = data
this.recents = recents
CodePudding user response:
Promise.all returns a promise that will fulfill with an array of the individual promises' results. You cannot destructure arbitarily named object properties from there - use array destructuring instead!
const [data, recents] = await Promise.all([
// ^ ^
this.$store.dispatch('algo', input),
this.$store.dispatch('algo', input)
])
this.data = data
this.recents = recents
or shorter without the temporary variables:
;[this.data, this.recents] = await Promise.all([
this.$store.dispatch('algo', input),
this.$store.dispatch('algo', input)
])
CodePudding user response:
I would assume that Promise.all resolves an array. So using object destructuring won't work.
Try array destructuring instead:
(async() => {
const [
data,
recents
] = await Promise.all([
Promise.resolve('data'),
Promise.resolve('recents')
]);
console.log(
data,
recents
);
})();
