1
function handler(){
    const results = [].map((item) => {
      a();
      b();
    })
    Promise.allSettled(results);
  }

  function a() {
    // returns promise
  }
  function b() {
    // returns promise
  }
}

How to modify the above code so that I can pass array of promises to promise.allSettled(results)

I tried this but not sure if this is correct way?

function handler() {
    const results = [].map((item) => {
      const out1 = a();
      const out2 = b();
      return [...out1, ...out2];
    })
    Promise.allSettled(results);
}
1
  • 2
    you want flatMap Commented Nov 22, 2022 at 17:01

2 Answers 2

1

Use flatMap

const a = [1,2,3,4,5,6];

const b = a.flatMap(v => [Promise.resolve(a), Promise.reject(a)])

Promise.allSettled(b).then(x => console.log(x));

Sign up to request clarification or add additional context in comments.

Comments

0

This should work

function handler() {
    const results = [].map((item) => {
      const out1 = a();
      const out2 = b();
      return [...out1, ...out2];
    })
    Promise.allSettled(results.flat());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.