Javascript 如何访问从Promise.all()返回的密钥

Javascript 如何访问从Promise.all()返回的密钥,javascript,object,Javascript,Object,如何访问从Promise.all()返回的对象中的项目 我想遍历整个数组,并从每个promise中获取返回的标题,但我无法访问promise{},然后访问其中的任何对象 [ Promise { { _id: 5e09e4e0fcda6f268cefef3f, title: 'dfgfdgd', shortDescription: 'gfdgdfg' }, qty: 1 }, Promise { { _id

如何访问从
Promise.all()
返回的对象中的项目

我想遍历整个数组,并从每个promise中获取返回的标题,但我无法访问
promise{}
,然后访问其中的任何对象

[
  Promise {
    {
      _id: 5e09e4e0fcda6f268cefef3f,
      title: 'dfgfdgd',
      shortDescription: 'gfdgdfg'
    },
    qty: 1
  },
  Promise {
    {
      _id: 5e09e507fcda6f268cefef40,
      title: 'Test product',
      shortDescription: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the '
    },
    qty: 1
  },
  Promise {
    {
      _id: 5e09e507fcda6f268cefef40,
      title: 'Test product',
      shortDescription: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the '
    },
    qty: 3
  }
]
编辑 这是创建promise数组的代码

const userId = req.user._id;
  try {
    const user = await User.findById(userId);
    const { inCart } = user;
    const results = [];
    for (let i = 0; i < inCart.length; i += 1) {
      results.push(Product.findById(inCart[i].itemId, 'title shortDescription').exec());
    }
    await Promise.all(results);
    for (let i = 0; i < results.length; i += 1) {
      results[i].qty = inCart[i].qty;
    }
    return res.render('shop/cart', { items: results });
  } catch (error) {
    console.log(error)
  }
const userId=req.user.\u id;
试一试{
const user=await user.findById(userId);
const{incontat}=用户;
常量结果=[];
对于(设i=0;i
承诺。所有
返回一个。获得承诺履行价值的唯一方法是使用履行处理程序

我想迭代整个数组,并获得每个承诺的标题

承诺没有头衔。如果实现值是具有
标题
属性的对象,则它们可能是

例如:

thePromise
.then(results => {
    // ...use `results` (an array) here...
    for (const {title} of results) {
        // ...use `title` here, it's each title in the array of objects...
    }
})
.catch(error => {
    // ...handle/report error here...
});
或者在
异步
函数中:

try {
    const results = await thePromise;
    for (const {title} of results) {
        // ...use `title` here, it's each title in the array of objects...
    }
} catch (error) {
    // ...handle/report error here...
}

(或
对于(wait thePromise的const{title})
,如果不需要,实际上不需要
结果
常量。)

首先,需要
等待
承诺,然后可以得到包含对象的数组

async function getData(){
    try{
        const data = await Promise.all()
        data[0].title // result : dfgfdgd
    }catch(e){
        // handle errors
    }
}

我猜是有了
Promise.all
就没有机会知道是哪一个调用了里面的
resolve
函数了

从文件中可以看出:

Promise.all()方法返回一个承诺,当作为iterable传递的所有承诺都已实现或iterable不包含任何承诺时,该承诺将实现。它拒绝的理由是第一个拒绝的承诺

在我的示例中,我创建了
Promise
元素,这些元素使用了更简单的对象,如
{title:'first'}
,以便更好地表示

要实现您的目标您需要处理每个
承诺
已解决的状态,例如在
forEach
中处理,就像在我的解决方案中一样,而不是使用
承诺.all()
。通过使用
然后在每次迭代中使用
,您可以访问已解析的对象属性

相反,您可以这样做-显然,您需要应用于您的结构:

const承诺=[
新承诺(解决=>{
设置超时(()=>{
解析({title:'first'})
}, 1200)
}),
新承诺(解决=>{
设置超时(()=>{
解析({title:'second'})
}, 800)
}),
新承诺(解决=>{
设置超时(()=>{
解析({title:'third'})
}, 2400)
}),
新承诺(解决=>{
设置超时(()=>{
解析({title:'fourth'})
}, 3200)
}),
];
然后(()=>console.log('all finished');

forEach(p=>p.then(d=>console.log(`${d.title}finished`))
结果
是一个承诺数组,它将保留该承诺数组-
承诺。所有
都不会改变该数组。它用结果值创建一个新数组,这就是
await
表达式将产生的结果。你应该使用

const userId = req.user._id;
try {
    const user = await User.findById(userId);
    const { inCart } = user;
    const promises = [];
    for (const item of inCart) {
        promises.push(Product.findById(item.itemId, 'title shortDescription').exec());
//      ^^^^^^^^
    }
    const results = await Promise.all(promises);
//        ^^^^^^^^^                   ^^^^^^^^
    // drop the qty assignment completely
    return res.render('shop/cart', { items: results });
} catch (error) {
    console.log(error)
}

这不是对象的有效表示法。你能展示构建这些的代码,让我们知道你在处理什么吗?我已经添加了创建它的代码。啊,这是你的问题:
结果
是一个承诺数组,它将保留承诺数组-
承诺。所有的
都不会改变数组。它用结果值创建一个新数组,这就是
await
表达式将产生的结果。是的,没错,我只需要将Promise all赋值给一个新数组。谢谢