Here is a snippet of my Angular 4 Service code:
@Injectable()
export class MyService {
private myArray: string[] = [];
constructor() { }
private calculate(result): void {
myArray.length = 0;
// Perform calculations and add results to myArray
}
public invokeCallBack(callBack: Function) {
// The callBack function returns an observable
// REST calls are made in the callBack function
callBack().subscribe(
(result) => {
// REST call is complete
this.calculate(result);
}
);
}
}
Other components frequently call invokeCallBack(callBack).
What happens if 2 or more rest calls finish simultaneously?
1) Will the method this.calculate(result) be called twice at the same time? This could lead to myArray having an inconsistent state due to potential race conditions. How can this issue be resolved?
2) Or will this.calculate(result) always be called synchronously? If so, only one calculation can occur at a time, ensuring that myArray remains in a consistent state.