I am currently working on the frontend development of a Web Application using Angular. My task involves saving data from an array into a database by making repeated API calls until all the array data is processed. I implemented the use of setTimeout()
in the code snippet below to ensure that each API call finishes processing an item from the array before moving on to the next. However, it seems like the setTimeout()
function is not functioning as expected and I'm unsure why this is happening. Any suggestions or insights would be greatly appreciated. Thank you in advance!
this.userEntitiesRoles.forEach(newRole => {
this.user.entity = newRole.entity;
this.user.role = newRole.role;
setTimeout(() => {
this.usersService.addUser(this.user, this.lang).subscribe({
next: (): void => {},
error: (err): void => {
if(err.status != 401) {
var displayMessage = this.translate.instant('ERRORS.GENERAL_ERROR').toUpperCase().concat(' \r\n').concat(err.message);
this.notify.error(displayMessage);
}
}
});
}, 1000);
});
addUser(user: IUser, lang: string): Observable<IUser> {
let payload = new FormData();
payload.append('emailID', `${user.email}`);
return this.http.post<IUser>(`${environment.SERVER_ROOT}/add?lang=${lang}`, payload)
.pipe(
catchError(this.handleError)
);
}