【发布时间】:2021-09-14 19:49:06
【问题描述】:
您好,我想基于数组并行发出请求 - 如下
1) 执行单一登录请求 2)对于基于iif条件的每个条目,取iif的结果并执行一系列操作 3)基于链操作的最终结果的补丁状态。
我试过的代码如下
this.questionAnswerService
.loginToQuriousApi()
.toPromise()
.then((response) => {
access_token = response.access_token;
console.log(access_token);
entries.forEach((entry) => {
return iif(
() => entry.extension == 'pdf',
defer(() => this.convertFile.getPdfContentInText(entry.id)),
defer(() => this.convertFile.getTextContent(entry.id))
).pipe(
concatMap((textContent: string) => {
console.log(textContent);
ctx.patchState({
qnaloading: true,
});
return this.convertFile.convertFileContent(textContent);
}),
concatMap((convertedObject) => {
return this.convertFile.uploadToQNA(
entry.id,
convertedObject,
access_token
);
}),
concatMap((response: any) => {
ctx.patchState({ loading: false });
return ctx.dispatch(new UpdateEntries([response.fileEntry]));
}),
catchError((e) => {
ctx.patchState({ loading: false });
return of('reject: ' + JSON.stringify(e));
})
);
});
})
.catch((e) => {
ctx.patchState({ loading: false });
return console.log('reject: ' + JSON.stringify(e));
});
这里的条目是一个对象数组。承诺会被执行,但 for 循环不会。
【问题讨论】:
-
在循环中使用。例如: for (const key in object) { if (Object.hasOwnProperty.call(object, key)) { const element = object[key]; } }