在javascript中,如何从映射内的异步函数中获取产品值
我有以下代码。我想获取product变量的结果,并将它们放入lineItems.data.map之外的数组中,这样我就可以解析该数组并发送一个包含其内容的请求。我有点困惑于怎么做。我尝试创建一个空数组并将其推入,但没有成功。任何帮助都会很好。谢谢 数组可以在第一个异步函数中,但我现在无法理解它在javascript中,如何从映射内的异步函数中获取产品值,javascript,async-await,stripe-payments,array.prototype.map,Javascript,Async Await,Stripe Payments,Array.prototype.map,我有以下代码。我想获取product变量的结果,并将它们放入lineItems.data.map之外的数组中,这样我就可以解析该数组并发送一个包含其内容的请求。我有点困惑于怎么做。我尝试创建一个空数组并将其推入,但没有成功。任何帮助都会很好。谢谢 数组可以在第一个异步函数中,但我现在无法理解它 async function (err, lineItems) { await lineItems.data.map(async (item) => { cons
async function (err, lineItems) {
await lineItems.data.map(async (item) => {
console.log(item);
const product = await stripe.products.retrieve(item.price.product);
return product;
});
// I want an array here containing the product
}
现在,
lineItems.data.map
返回一个承诺数组。你不能直接等待,但是承诺。所有的都会让你等待一系列的承诺
异步函数foo(err,lineItems){
const arr=wait Promise.all(lineItems.data.map)((item)=>{
返回条带.产品.检索(项目.价格.产品);
}));
//解析数组应该在这里
控制台日志(arr);
}
foo();
下面是一个简单的例子,展示了这一切:
constprom=(num)=>newpromise(res=>res(num*2));
异步函数foo(){
const vals=wait Promise.all([1,2,3].map(x=>prom(x));
控制台日志(VAL);
}
foo()代码>这是否回答了您的问题?这回答了你的问题吗?