Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
Save the Promise.all into two variables

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
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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)
])
about 4 years ago · Juan Pablo Isaza Relatório

0

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
  );

})();

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda