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

174
Visualizações
How do I resolve the following promises?

I'm trying to resolve the promise in the getABC function, but the local const variable b returns undefined. How do I implement the callback function used by the getB function so that it returns the destructured array item with the value of 'B'.

const [A, B, C] = ['A', 'B', 'C'];

function getA() {
  return A;
}

function getB(callback) {
  setTimeout(() => {
    callback(B);
  }, 10);
}

function getC() {
  return Promise.resolve().then(() => C);
}

function getABC() {
  return new Promise((resolve, reject) => {
    const a = getA();
    const b = getB(b => b);
    const c = getC().then(item => item).catch(err => console.log(err));

    resolve([a, b, c])
  });
}

getABC().then((arr) => console.log(arr));

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

0

You also have to wrap setTimeout inside a Promise, and call Promise.all before resolve:

const [A, B, C] = ['A', 'B', 'C'];

function getA() {
  return A;
}

function getB(callback) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(callback(B));
    }, 10);
  });
}

function getC() {
  return Promise.resolve().then(() => C);
}

function getABC() {
  return new Promise((resolve, reject) => {
    const a = getA();
    const b = getB(b => b);
    const c = getC().then(item => item).catch(err => console.log(err));

    resolve(Promise.all([a, b, c]));
  });
}

getABC().then(arr => console.log(arr));

All of these can truly be simplified if you use async/await.

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