Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何正确使用forkJoin和switchMap并承诺调用两个api?_Javascript_Node.js_Angular_Angular Material_Fork Join - Fatal编程技术网

Javascript 如何正确使用forkJoin和switchMap并承诺调用两个api?

Javascript 如何正确使用forkJoin和switchMap并承诺调用两个api?,javascript,node.js,angular,angular-material,fork-join,Javascript,Node.js,Angular,Angular Material,Fork Join,我正在Angular 9中进行api调用,如下所示: import {SubSink} from 'subsink'; ... ... async clickButton() { for (let i = 0; i < this.Id.length; i++) { const hostId = await this.serviceA.Status(this.hostName[i]); this.subs.sink = this.serviceB.cr

我正在
Angular 9
中进行api调用,如下所示:

import {SubSink} from 'subsink';
...
...
async clickButton() {
    for (let i = 0; i < this.Id.length; i++) {
        const hostId = await this.serviceA.Status(this.hostName[i]);
        this.subs.sink = this.serviceB.createDbEntry(hostId))
            .subscribe(s => {
                if (i === this.Id.length - 1) {
                    this.dialog.close();
                }
            });
    }
}
serviceastatus
的实现如下:

import {forkJoin} from 'rxjs';
import {switchMap} from 'rxjs/operators';

forkJoin(this.hostName.slice(0, this.Id.length).map(hostName => {
  return this.serviceA.Status(hostName).pipe(
    switchMap(hostId => this.serviceB.createDbEntry(hostId).pipe(map((dbEntry) => ({dbEntry, hostId})))),
    switchMap(resp => this.serviceC.runEntry(resp.hostId))
  )
})).subscribe(() => this.dialog.close());
public status<T>(host: string) {
    const hostString = host.toString();
    const Url = `${this.api}/${hostString}`;
    const headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.append('Accept', 'application/json');

    return this.http.get<rData<T>>(Url).toPromise();
}
forkJoin Deprecated symbol used, consult docs for better alternative deprecated export function forkJoin<any>( sources: any):Observable<unknown[]>
公共状态(主机:字符串){
const hostString=host.toString();
常量Url=`${this.api}/${hostString}`;
const headers=新的HttpHeaders();
headers.append('Content-Type','application/json');
headers.append('Accept','application/json');
返回此.http.get(Url.toPromise();
}
我收到如下警告:

import {forkJoin} from 'rxjs';
import {switchMap} from 'rxjs/operators';

forkJoin(this.hostName.slice(0, this.Id.length).map(hostName => {
  return this.serviceA.Status(hostName).pipe(
    switchMap(hostId => this.serviceB.createDbEntry(hostId).pipe(map((dbEntry) => ({dbEntry, hostId})))),
    switchMap(resp => this.serviceC.runEntry(resp.hostId))
  )
})).subscribe(() => this.dialog.close());
public status<T>(host: string) {
    const hostString = host.toString();
    const Url = `${this.api}/${hostString}`;
    const headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.append('Accept', 'application/json');

    return this.http.get<rData<T>>(Url).toPromise();
}
forkJoin Deprecated symbol used, consult docs for better alternative deprecated export function forkJoin<any>( sources: any):Observable<unknown[]>
forkJoin不推荐使用的符号,请参考文档以获得更好的替代不推荐使用的导出函数forkJoin(来源:any):可观察
我也得到了一个错误:
TS2339:类型“Promise>”上不存在属性“pipe”。

此处:

const hostId = await this.serviceA.Status(this.hostName[i]);


在第一行中,您将
serviceA.Status()
视为承诺。在第二个例子中,你把它当作一个可观察的。我假设第一个是正确的(来自错误消息)。快速修复:使用
from
操作符将承诺转换为可观察的。好的解决方法:不要将promise范例与RxJS范例混为一谈,坚持一个(最好坚持Rx),否则它可能会变得非常混乱。

你能展示一下serviceA.Status的实现吗?看起来它不返回可观察的。您从何处导入forkJoin?”rxjs’?你可以向switchMap回复承诺,但你不能在不加入From的情况下通过管道将其关闭。我已经更新了我的问题。你能提供一个有效的答案吗?我不需要使用
forkJoin
,也不需要为其他方法打开