使用wait更新大量项目列表,如何加快速度



我正在更新一个大的项目列表并设置价格。

对于。。。

currentItem.attributes.menuItemPrice10 = currentItem.attributes.menuItemPrice1 + .28;
await this.menuItemsService.save(currentItem);

这可以正常工作,但Angular似乎有有限的线程选项。

如果我取消等待,那么它就会在列表中弹出,但检查日志并不会更新所有内容——所以它就好像因为我没有等待而放弃了一些更新,对吧?

因此,另一种选择似乎是网络工作者,但它表示,这不支持作为网络工作者运行"自己",并且有一些平台限制。不确定这意味着什么,所以寻找如何加快速度的最佳实践?

感谢

我假设您的save是异步操作,例如网络请求。如果它真的使用了像fs.writeFileSync这样的东西来阻止整个过程,那么你就无法加快它的速度。

有一个Promise.all,它允许您等待一个Promise数组的解析。将其与.map相结合,您可以对项目列表执行批量操作:

await Promise.all(items.map(item => this.menuItemService.save(item)));
// or if it's just a hardcoded small amount of operations:
await Promise.all([
this.menItemService.save(previousItem),
this.menItemService.save(currentItem),
this.menItemService.save(nextItem),
]);

最新更新