【发布时间】:2017-08-28 00:25:59
【问题描述】:
我正在尝试连续进行几个 Web 服务调用,但我遇到的问题是第二次调用将在 .subscribe 执行第一次调用之前进行。我需要发生 .subscribe 以设置 this.saveValue,因为 this.saveValue 在第二个 Web 服务调用的 JSON 中传递。
此代码当前发生的情况是,将进行第一次调用,然后进行第二次调用,然后再设置 saveValue。
组件.ts
busy: Subscription;
saveValue: string;
submitButton_Click():void{
try{
// First Web Service Call
this.busy = this.service.firstMethod(this.object)
.first()
.subscribe(result => {
if(result.length > 0){
this.saveValue= result; // VALUE SET FOR USE OF NEXT WEB SERVICE CALL
console.log('Worked');
}
else{
console.log('Failed');
}
})
for(let current of this.array){
let temp= new Object();
temp.id = this.saveValue;
// Second Web Service Call
this.busy = this.service.secondMethod(temp)
.first()
.subscribe(result => {
if(result.valueOf() != false){
console.log('Worked');
}
else{
console.log('Failed');
}
})
}
服务.ts
// First Method
public firstMethod(object: Object): Observable<string>{
return this.client.post<Result>(URL + '/add', object, new Result(), this.token.id)
.map(result => {
let temp = new Object().deserialize(result.data);
return temp.id;
}, this)
.catch(this.handleError);
} // This works and returns proper value
// Second Method (Needs this.saveValue as part of the passed object)
public secondMethod(object: Object): Observable<boolean> {
return this.client.post<Result>(OTHERURL + '/add', object, new Result(), this.token.id)
.map(result => result.success, this)
.catch(this.handleError);
}
感谢您的帮助!
【问题讨论】:
标签: javascript angular typescript