Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda