承诺{< pending> }-试图等待.map [英] Promise { <pending> } - Trying to await for .map
问题描述
我正在使用.map
绘制新对象并将旧价格添加到地图中.
I am using .map
to map out a new object and to add the old price to the map.
我正在对数据映射使用Async/Await,这是我的代码:
I am using Async/Await with my datamaps, here is what my code looks like:
let datasets = await changes.map(async (data) => {
let products = {};
let last = await models.prices.findOne({
where: {
productId: data.productId,
sourceId: data.sourceId
},
order: [['createdAt', 'DESC']],
limit: 1,
offset: 1
});
products.name = data.product.name;
products.price = data.price;
products.sku = data.product.sku;
products.source = data.source.name;
products.link = data.link;
products.diff = last.price;
return products;
});
changes
是最近24小时内发现的所有价格变动.
changes
are all of the prices changes found in the last 24 hours.
last
包含上次发现特定产品的价格变化的时间.
last
contains the previous time a price change was found of the particular product.
return products
不在等待中,因此我收到了Promise { <pending> }
邮件的垃圾邮件.如果使用console.log(last)
,它将在内部运行,但是我无法找出减慢返回速度的正确方法.
The return products
isn't waiting, so I get a spam of Promise { <pending> }
messages. If I use a console.log(last)
it's working inside, but I cannot figure out the correct way to slow down the return.
products.diff = last.price
是需要填充才能使此有效的一件.有什么想法吗?
products.diff = last.price
is the one piece that needs to fill for this to be valid. Any ideas?
推荐答案
await
等待Promises,但Array.prototype.map
返回一个新的Promises数组.您需要用Promise.all
await
awaits on Promises but Array.prototype.map
returns a new array of Promises. You need to wrap it with Promise.all
let datasets = await Promise.all(changes.map(async (data) => {
let products = {};
let last = await models.prices.findOne({
where: {
productId: data.productId,
sourceId: data.sourceId
},
order: [['createdAt', 'DESC']],
limit: 1,
offset: 1
});
products.name = data.product.name;
products.price = data.price;
products.sku = data.product.sku;
products.source = data.source.name;
products.link = data.link;
products.diff = last.price;
return products;
}));
这篇关于承诺{< pending> }-试图等待.map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!