What methods can I use to optimize the process of updating a vast list of items with await?

I have a large list of items to update and I need to set prices.

for...

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

While this process works fine, I have noticed that Angular has limited threading options.

If I remove the await keyword, the script quickly goes through the list, but it doesn't seem to update everything based on the logs - it's like some updates are being skipped because I didn't wait for them to complete, right?

So, I'm considering using web workers as an alternative, but I read that it does not support running 'itself' as a web worker and may have platform limitations. I'm not exactly sure what that means, so I'm seeking advice on the best practice to speed up this process.

Thank you

Answer №1

If your save function is asynchronous, such as a network request, then you may be able to improve its performance. However, if it utilizes a synchronous operation like fs.writeFileSync, which blocks the entire process, there is limited room for optimization.

You can make use of Promise.all to handle multiple asynchronous operations concurrently. By combining this with .map, you can efficiently execute bulk actions on arrays of items:

await Promise.all(items.map(item => this.menuItemService.save(item)));
// Alternatively, for a small number of predefined operations:
await Promise.all([
    this.menItemService.save(previousItem),
    this.menItemService.save(currentItem),
    this.menItemService.save(nextItem),
]);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vue encountered a double loading issue when utilizing a library compiled with Webpack

I am facing an issue with my TypeScript library of Vue components that gets compiled into a single JS file using Webpack. The problem arises when the TypeScript project consuming this library also depends on Vue. Upon running the application, I noticed tha ...

Issue: formGroup requires an input of type FormGroup. Please provide one; Error: Unable to access property 'controls' as it is undefined

In the process of building a login and registration form using Angular with .Net core, I encountered an error upon running the program. The error is showing up in the Browser Console tab. This is my userlog.component.ts file: import { Component, OnInit, O ...

Executing service calls within providers - a guide

Imagine I have a function named service(). This function is designed to request data from a service. Does anyone have experience with calling a service within providers? export function service(){ // initiate service call } @Component({ ... provider ...

Issue with Angular Material Auto Complete not selecting items when filtered

Encountered a problem with the mat-autocomplete control in Angular Material where it fails to include the CSS class "mdc-list-item--selected" on the mat-option and also does not add the mat-pseudo-checkbox to a selected option when the contents are display ...

Ensure all promises are resolved inside of for loops before moving on to the next

Within my angular 11 component, I have a process that iterates through elements on a page and converts them from HTML to canvas to images, which are then appended to a form. The problem I am encountering is that the promise always resolves after the ' ...

Encountering an issue with Typescript and SystemJS: Struggling to locate a

After developing a module, I decided to move it out of the app and into node_modules. However, I encountered an error error TS2307: Cannot find module 'bipartite-graph'.. In this case, bipartite-graph is actually my own module. Here is the conte ...

Utilize ngrx effects for making API requests without storing the response in the store

Currently, I am working on an Angular project that utilizes ngrx/store. One of my components requires data from the backend. The usual flow for this scenario is as follows: dispatch a Action trigger an Effect --> make a call to the backend update the ...

Having difficulty disassociating the radio button from the label

<div class="questions" style="font-size:13pt;margin-left:20px;margin-bottom:10px;"> <p class="question" id="ques{{i+1}}" style="font-size:17pt;font-weight:normal">{{i+1+". "+a.question}}</p> <img *ngIf="isImage[i]" [src]="a.image" ...

What is the best way to view a PDF file stored locally in a web browser using Angular 6?

Whenever I attempt to click on this link, it redirects me and then reloads the page showing a blank http://localhost:4200/ page instead of displaying the PDF. This functionality worked perfectly on another project, and despite replicating the same steps, n ...

Ways to avoid inaccurate information in Angular without relying on form functionalities

In my Angular application, I am looking for a way to easily validate number inputs without using forms. Specifically, I want to prevent users from entering values below 1 or greater than 99. I have successfully developed a component that displays dynamic d ...

Exploring the SetCustomValidity Function in Angular

I am trying to utilize the html setCustomValidity method for setting up business validation, however the form is not blocking the custom validity. How can I implement this function in Angular? Here is the code snippet: export class AutoComponent { au ...

Error message: "Lazy-loaded modules encounter a TypeError stating that '' is not a function when accessed through NGINX."

Hey, I've got some distribution files for you to check out: AOT-enabled dist with Lazy Modules No AOT Lazy Modules dist AOT without Lazy Modules dist Here's what's been going on: When served locally with webpack-dev-server or live-serve ...

Issues have arisen where the modal popup in Bootstrap and Angular is failing to appear

Displayed below is a table with a modal popup created using bootstrap 4 within an angular environment. <tbody *ngFor="let data of json | sizeFilter : sizeInput"> <tr> <td (click)="onUpdateClick(data.hash , data. ...

Issue arising from frequent refresh token expiration in Angular 8

Currently, my setup involves AWS Cognito for oauth authentication. Upon login, Cognito returns a token that needs to be refreshed every hour. I implemented logic using setTimeout to handle this token refresh every hour, but this method fails upon page refr ...

Transmitting a File from Angular to Nodejs - Issue with accessing the 'headers' property

I've encountered an issue while trying to pass my data along with a file to my nodejs backend. I'm currently utilizing Azure functions to execute my nodejs code. Whenever I attempt to pass the data with a file, I receive a Cannot read property &a ...

Using ngrx to automatically update data upon subscription

Background The technology stack I am using for my application includes Angular 4.x, ngrx 4.x, and rxjs 5.4.x. Data is retrieved from a websocket as well as a RESTful API in order to share it between multiple components through ngrx. Currently, data is ref ...

Unlocking the Power of Dependent Types in TypeScript: Unveiling Type by Property Name Declaration

In an attempt to tie the types to the arguments passed, consider the following example: type NS = "num" | "str" type Data<T extends NS> = T extends "num" ? number : string type Func<T extends NS> = (x: Data<T> ...

Bring in a template in TypeScript with no need for require statement

Currently, I'm utilizing TypeScript along with Angular 1.5 component. In my scenario, I have a custom decorator in place through which I send templates via require function. The setup is functional; however, I am consistently receiving a tslint warnin ...

Having trouble migrating Angular with Nativescript? Encounter an issue parsing the angular.json file

Attempting to enhance Angular with Nativescript by following the provided instructions has led to a puzzling issue. Whenever the command to add nativescript-schematics (ng add @nativescript/schematics) is executed, an error message stating "File angular.js ...

Creating a gallery with thumbnail images using the ngx-swiper-wrapper package

I'm attempting to create an image slider with thumbnails using ngx-swiper-wrapper, similar to the example shown here: Has anyone successfully accomplished this? I am having trouble finding detailed instructions on how to create multiple sliders with ...