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

172
Views
javascript - not able to execute the promise from array one by one

I found that I cannot resolve the promises one by one.

This is my code

const promises = mkt.map((marketItem) => {
  return context.program.account.chain
    .fetch(marketItem[0].instrument)
    .then((instrumentRes) => {
      return Test(context, marketItem[0]).then(
        testResult => {
          return Promise.all([
            functionA(),
            functionB(),
          ]).then((result) => {
            return result
          });
        }
      );
    });
});
console.log(promises)
for (let i=0; i < promises.length; i++) {
  const val = await promises[i]();
  console.log(val);
}

error

promises[i]() is not a function

Why is that?How can I solve it?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

promises[i]() is not a function

Correct, this is because promises is an array of Promise objects, not functions.

You can dump this promises array into Promise.all and wait for them all to resolve.

Promise.all(promises)
  .then(resolvedPromises => {
    // handle array of resolved promises
  });

but I am working on some real time update stuff. If I use Promise.all it will throw error Too many request, so I want to do it one by one to see the effect

For this then I think you want to iterate on the mkt array and wait for each created Promise to resolve. Refactor the mapping callback into a standalone function that you can manually invoke within the for-loop, awaiting the the Promise to settle.

const request = (marketItem) => {
  return context.program.account.chain
    .fetch(marketItem[0].instrument)
    .then((instrumentRes) => Test(context, marketItem[0]))
    .then(testResult => Promise.all([functionA(), functionB()]))
    .then((result) => result);
}

for (let i=0; i < mkt.length; i++) {
  try {
    const val = await request(mkt[i]);
    console.log(val);
  } catch(error) {
    // handle any error
  }
}
about 4 years ago · Juan Pablo Isaza Report

0

Just removing the () from the await call should already work.

  const val = await promises[i];

However keep in mind that all the promises have already been "started" (I don't know a better word for that) at that point and you're just retrieving the results or an error with the await call.

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!