Javascript 角4中是否可能存在竞争条件
我的服务:Javascript 角4中是否可能存在竞争条件,javascript,angular,rest,typescript,race-condition,Javascript,Angular,Rest,Typescript,Race Condition,我的服务: @Injectable() export class MyService { private myArray: string[] = []; constructor() { } private calculate(result): void { myArray.length = 0; // Do some calculations and add results in myArray } public invokeCallBack(call
@Injectable()
export class MyService {
private myArray: string[] = [];
constructor() { }
private calculate(result): void {
myArray.length = 0;
// Do some calculations and add results in myArray
}
public invokeCallBack(callBack: Function) {
// the function callBack returns an observable
// Rest calls are done in the callBack function
callBack().subscribe(
(result) => {
// Rest call is finished
this.calculate(result);
}
);
}
}
其他组件多次调用invokeCallBack(回调)
如果两个(或更多)rest调用同时完成,会发生什么情况
1) 这个计算(结果)的方法会同时调用两次吗?如果是这种情况,myArray可能具有不一致的状态,因为两个计算同时进行(=>竞争条件)。这个问题怎么解决
2) 或者这个.calculate(result)总是被称为同步的吗?如果是这种情况,一次只能进行一次计算,因此myArray始终(保证)处于一致状态。假设在
calculate
中没有异步代码,则在再次调用之前,该方法将始终运行到完成
calculate
的两个单独的“实例”不可能同时运行
这是因为JavaScript(在浏览器中)。假设
calculate
中没有异步代码,那么在再次调用之前,该方法将始终运行到完成
calculate
的两个单独的“实例”不可能同时运行
这是因为JavaScript(在浏览器中)