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

138
Visualizações
How to get the output in the same order as the function available in the array in JavaScript

Only change the ABC function, how can I get the output in the same order as the function present in the D variable. I have tried using async-await but getting the output as ['123', 'ankit', '345'] because of the timeout event.

const A = (dev) => {
  setTimeout(() => {
    dev('ankit')
  }, 300)
}

const B = (dev) => {
  setTimeout(() => {
    dev('123')
  }, 50)
}

const C = (dev) => {
  setTimeout(() => {
    dev('345')
  }, 500)
}

const D = [A, B, C]

const ABC = (args, dev) => {
  // write your code here
  Promise.all(D.map(async (fun1) => {
    return await fun1(dev)
  }))
}

ABC(D, (result) => {
  console.log('result:', result) // ['ankit', 123, 345]
})

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since the order of execution depends on the index in the functions array not the execution time, you can simply use a for loop:

const A = dev => new Promise(resolve => 
  setTimeout(() => { dev('ankit'); resolve(); }, 300)
);
const B = dev => new Promise(resolve => 
  setTimeout(() => { dev('123'); resolve(); }, 50)
);
const C = dev => new Promise(resolve =>
  setTimeout(() => { dev('345'); resolve(); }, 500)
);

const ABC = async (args, dev) => {
  for(let i = 0; i < args.length; i++) {
    await args[i](dev);
  }
}

const run = (async () => { 
  await ABC([A, B, C], result => console.log('result:', result));
})();

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