简体   繁体   English

如何解决嵌套的承诺?

[英]How to resolve nested promises?

I'm trying to resolve multiple promises scattered across objects inside an array.我正在尝试解决分散在数组内对象中的多个承诺。 Here is a simplistic version of my code:这是我的代码的简化版本:

 const getOrderSum = order_id => { return new Promise(resolve => { setTimeout(() => resolve(34), 1000) }) } const customer_array = [ { customer_id: '19847743234730384', name: 'Customer 1', orders: [ { order_id: '98749873244324', price_per: 12 }, { order_id: '9874987323545', price_per: 16 } ] }, { customer_id: '123454351234123', name: 'Customer 2', orders: [ { order_id: '918741433423', price_per: 6 } ] } ] const result_array = customer_array.map(customer => { const promises = customer.orders.map(async order => { order.order_total = await getOrderSum(); return order; }); customer.orders = promises; return customer; }) console.log(result_array);

Inside the map loops I want to make a call to an async function that returns an order_total for each order.在 map 循环中,我想调用一个异步 function,它为每个订单返回一个order_total result_array looks like this after the code runs:代码运行后, result_array如下所示:

[
  {
    customer_id: '19847743234730384',
    name: 'Customer 1',
    orders: [ [Promise], [Promise] ]
  },
  {
    customer_id: '123454351234123',
    name: 'Customer 2',
    orders: [ [Promise] ]
  }
]

How do I resolve all of those 3 promises at once and get this:我如何一次解决所有这三个承诺并得到这个:

[
    {
        customer_id: '19847743234730384',
        name: 'Customer 1',
        orders: [
            {
                order_id: '98749873244324',
                price_per: 12,
                order_total: 34
            },
            {
                order_id: '9874987323545',
                price_per: 16,
                order_total: 34
            }
        ]
    },
    {
        customer_id: '123454351234123',
        name: 'Customer 2',
        orders: [
            {
                order_id: '918741433423',
                price_per: 6,
                order_total: 34
            }
        ]
    }
]

Where would I use Promise.all considering that the promises are not in one array?考虑到承诺不在一个数组中,我将在哪里使用Promise.all

Please check the code below:请检查以下代码:

 const getOrderSum = order_id => { return new Promise(resolve => { setTimeout(() => resolve(34), 1000) }) } const customer_array = [{ customer_id: '19847743234730384', name: 'Customer 1', orders: [{ order_id: '98749873244324', price_per: 12 }, { order_id: '9874987323545', price_per: 16 } ] }, { customer_id: '123454351234123', name: 'Customer 2', orders: [{ order_id: '918741433423', price_per: 6 }] } ] let promises = []; const result_array = customer_array.map(customer => { customer.orders.map((order) => { const p = getOrderSum().then((order_total) => { order.order_total = order_total; }); promises.push(p); return order; }); return customer; }); Promise.all(promises).then(() => { console.log(result_array) });

  1. Make customer_array.map as async .customer_array.map设为async
  2. Use await Promise.all with customer.orders.map so that it will return result instead of Promise .使用await Promise.allcustomer.orders.map以便它返回结果而不是Promise
  3. As we have make customer_array.map as async and it is not wrapped inside async function so we need to resolve it with Promise.all().then() .由于我们已将customer_array.map设为async ,并且它未包装在async function 中,因此我们需要使用Promise.all().then()resolveWrite your required code inside then . then在里面写下你需要的代码。

Try it below.在下面试试。

 const getOrderSum = order_id => { return new Promise(resolve => { setTimeout(() => resolve(34), 1000) }) } const customer_array = [{ customer_id: '19847743234730384', name: 'Customer 1', orders: [{ order_id: '98749873244324', price_per: 12 }, { order_id: '9874987323545', price_per: 16 } ] }, { customer_id: '123454351234123', name: 'Customer 2', orders: [{ order_id: '918741433423', price_per: 6 }] } ] const promises = customer_array.map(async customer => { customer.orders = await Promise.all(customer.orders.map(async order => { order.order_total = await getOrderSum(); return order; })); return customer; }); Promise.all(promises).then(results => console.log(results));

You can use promise.all for that and then use use.then method to resolve the value您可以为此使用 promise.all ,然后使用 use.then 方法来解析该值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM