javascript - How to flatten a promise.all response - Stack Overflow

I have a list of products and for each product im making an api call to get the products subscription.c

I have a list of products and for each product im making an api call to get the products subscription.

   const subscriptions = await Promise.all(products.flatMap((p: any) => {
        const { username, password } = p.credentials;
        return GetSubscription(username, password);
    }));
    console.log(subscriptions);

Actual:

[ [ {}, {}, {}, ] ]

now you can see i have a double nested array here and i don't need the outer array, how can i flatten this array, i assumed using flatMap here might help me but it did not.

Expected:

[ {}, {}, {}, ]

Ofcourse i could simply do subscriptions[0] but im trying to avoid that if possible.

I have a list of products and for each product im making an api call to get the products subscription.

   const subscriptions = await Promise.all(products.flatMap((p: any) => {
        const { username, password } = p.credentials;
        return GetSubscription(username, password);
    }));
    console.log(subscriptions);

Actual:

[ [ {}, {}, {}, ] ]

now you can see i have a double nested array here and i don't need the outer array, how can i flatten this array, i assumed using flatMap here might help me but it did not.

Expected:

[ {}, {}, {}, ]

Ofcourse i could simply do subscriptions[0] but im trying to avoid that if possible.

Share Improve this question asked Jan 27, 2020 at 10:36 KayKay 19.7k72 gold badges184 silver badges301 bronze badges 2
  • 3 Array.prototype.flat() - developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Bloatlord Commented Jan 27, 2020 at 10:38
  • ah the simple .flat does not make my api call GetSubscription so it did not work – Kay Commented Jan 27, 2020 at 10:55
Add a ment  | 

3 Answers 3

Reset to default 8

Use the spread operator

async function func() {
  return 'A';
}

(async() => {
  const [...ret] = await Promise.all([
    func(),
    func(),
    func(),
  ]);

  console.log(ret);
})();

Or

async function func() {
  return 'A';
}

(async() => {
  const ret = await Promise.all([
    func(),
    func(),
    func(),
  ]);

  console.log(ret.reduce((tmp, x) => [...tmp, x], []));
})();


As you said in your ment, specifying ES2019 in your tsconfig, you can use Array.flat :

async function func() {
  return 'A';
}

(async() => {
  const ret = await Promise.all([
    func(),
    func(),
    func(),
  ]);

  console.log(ret.flat());
})();

This should help you.

const input = [[{}, {}, {}], [{"hi": "bye"}]];


console.log(input.flat());

Array.flatMap() is the same as Array.map().flat(). So await Promise.all(Array.map().flat()) will be applied to the result of flat. When the map callback is asynchronous you need to resolve the result of map before calling flat. In that case you would need to use (await Promise.all(Array.map())).flat().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743752473a4501154.html

相关推荐

  • javascript - How to flatten a promise.all response - Stack Overflow

    I have a list of products and for each product im making an api call to get the products subscription.c

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信