讨论两种情况:
一次发送多个请求,等多个请求都返回后再处理下一步,各个请求间是独立的
Promise.all(iterable) 返回一个[],一旦有任何一个iterable里面的promise对象失败则立即触发该promise对象的失败
Rxjs 的 forkjoin
observers = fieldsObserver.((field) => { Observable(observer => { ..(fieldobserver)}) })(...observers).(data => { obj: = {}data.((itemindex) => { obj[fieldsObserver[index].] = item})callback(obj)})
async await (ES6的新标准,把异步的转化成同步的)
let test = async () => {
try{
masterDetailFormInfo = await new Promise((resolve, reject) => {
this.formService.publicGetFormInfo(field.settings.formId).subscribe(obj => {
if(obj.success){
resolve(obj.form);
}else{
reject();
}
});
});
results = await new Promise((resolve, reject) => {
let params: any = {
formId: masterDetailFormInfo.id,
tags: [this.formInfo.data.id+'-'+ field.uid]
};
params.start = 0;
params.limit = 10000;
this.formDataService.formDataSearch(params).subscribe(obj => {
if(obj.success){
let data = this.formHelper.generateRecs(obj.results, masterDetailFormInfo);
resolve(data);
}else{
reject();
}
});
});
}catch(e){
return ;
}
}
test().then((data) => {})
一次发送多个请求,但是下一个请求都需要用上一个请求的返回值,最后只输出最后一个请求的值
Rxjs mergMap
..<>(..[]).( ((obj: ) => { (!obj.){ Observable(observer => observer.({:}))} req = HttpRequest(obj.formData{ :HttpHeaders({: obj.opToken}) })..(req).(((event: HttpEvent<{}>) => { if(!event.body['success']){ return new Observable(observer => observer.next({'success':false})); } const params = new FormData(); params.append('fileKey', event.body['fileKey']); params.append('formId', this.formInfo.id); params.append('fieldId', this.fileField['uid']); //send fileKey to backend return this.http.post(this.globalConst.urls['formUploadFile'], params); } { Observable()} })) }) ).((obj: ) => { (obj[]){}{}})
async await (ES6的新标准,把异步的转化成同步的)