Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!