我可以调用observable中的API以特定的顺序吗?

huangapple go评论50阅读模式
英文:

can I call the apis in observable in a certain sequence/order

问题

我正在学习angular中的Observable主题。我尝试在Observable内部调用API,并希望它们按照我指定的顺序运行。

下面是我的组件类代码:

import { HttpClient } from '@angular/common/http';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-flav4',
  templateUrl: './flav4.component.html',
  styleUrls: ['./flav4.component.css']
})
export class Flav4Component implements OnInit, OnDestroy {
  orderStatus!: any;
  data!: Observable<any>;
  constructor(private httpcli: HttpClient) { }
  ngOnInit(): void {
    this.data = new Observable(observer => {
      this.httpcli.get("http://localhost:9898/api/orderStart", { headers: { "content-type": "application/json" } }).subscribe(data => {
        console.warn(data);
        this.orderStatus = data;
        observer.next(this.orderStatus);
      });
      this.httpcli.get("http://localhost:9898/api/orderWait", { headers: { "content-type": "application/json" } }).subscribe(data => {
        console.warn(data);
        this.orderStatus = data;
        observer.next(this.orderStatus);
      });
      this.httpcli.get("http://localhost:9898/api/orderComplete", { headers: { "content-type": "application/json" } }).subscribe(data => {
        console.warn(data);
        this orderStatus = data;
        observer.next(this.orderStatus);
      });
    });
    this.data.subscribe(val => this.orderStatus = val);
  }
  ngOnDestroy(): void {
  }
}

我期望控制台输出为:

{"status": "orderStarted"}
{"status": "orderWaiting"}
{"status": "orderCompleted"}

然而,我得到了以下输出:

{"status": "orderStarted"}
{"status": "orderCompleted"}
{"status": "orderWaiting"}

为了获得所期望的输出,即按照特定的顺序或序列运行API,你应该做以下更改:

你可以使用concatMap 操作符来确保API按照你的顺序运行。例如:

import { concatMap, switchMap } from 'rxjs/operators';

// ...

this.data = new Observable(observer => {
  this.httpcli.get("http://localhost:9898/api/orderStart", { headers: { "content-type": "application/json" } }).pipe(
    concatMap(data => {
      console.warn(data);
      this.orderStatus = data;
      observer.next(this.orderStatus);
      return this.httpcli.get("http://localhost:9898/api/orderWait", { headers: { "content-type": "application/json" });
    }),
    concatMap(data => {
      console.warn(data);
      this.orderStatus = data;
      observer.next(this.orderStatus);
      return this.httpcli.get("http://localhost:9898/api/orderComplete", { headers: { "content-type": "application/json" });
    })
  ).subscribe(data => {
    console.warn(data);
    this.orderStatus = data;
    observer.next(this.orderStatus);
  });
});

// ...

这样,你可以确保API按照你的指定顺序运行。

英文:

I am learning angular Observable topic. I was trying to call apis inside Observable and hoping that they will run in an order in which I have stated them.

Below is my component class code:

import { HttpClient } from &#39;@angular/common/http&#39;;
import { Component, OnDestroy, OnInit } from &#39;@angular/core&#39;;
import { Observable } from &#39;rxjs&#39;;

@Component({
  selector: &#39;app-flav4&#39;,
  templateUrl: &#39;./flav4.component.html&#39;,
  styleUrls: [&#39;./flav4.component.css&#39;]
})
export class Flav4Component implements OnInit, OnDestroy {
  orderStatus!: any;
  data!: Observable&lt;any&gt;;
  constructor(private httpcli: HttpClient) { }
  ngOnInit(): void {
    this.data = new Observable(observer =&gt; {
      this.httpcli.get(&quot;http://localhost:9898/api/orderStart&quot;, { headers: { &quot;content-type&quot;: &quot;application/json&quot; } }).subscribe(data =&gt; {
        console.warn(data);
        this.orderStatus = data;
        observer.next(this.orderStatus);

      });
      this.httpcli.get(&quot;http://localhost:9898/api/orderWait&quot;, { headers: { &quot;content-type&quot;: &quot;application/json&quot; } }).subscribe(data =&gt; {
        console.warn(data);
        this.orderStatus = data;
        observer.next(this.orderStatus);

      });
      this.httpcli.get(&quot;http://localhost:9898/api/orderComplete&quot;, { headers: { &quot;content-type&quot;: &quot;application/json&quot; } }).subscribe(data =&gt; {
        console.warn(data);
        this.orderStatus = data;
        observer.next(this.orderStatus);

      });
    });
    this.data.subscribe(val =&gt; this.orderStatus = val);
  }
  ngOnDestroy(): void {
  }
}

I was expecting console output to be

{&quot;status&quot;: &quot;orderStarted&quot;}
{&quot;status&quot;: &quot;orderWaiting&quot;}
{&quot;status&quot;: &quot;orderCompleted&quot;}

However I got below output

{&quot;status&quot;: &quot;orderStarted&quot;}
{&quot;status&quot;: &quot;orderCompleted&quot;}
{&quot;status&quot;: &quot;orderWaiting&quot;}

What should I change in this code in order to get desired output i.e to run APIs in a certain order or sequence.

答案1

得分: 1

这是正常行为,因为HTTP请求是异步的。

您可以尝试使用mergeMap,也被称为RxJS库中的flatmap,来链接它们。我建议您也查看一下行为。

参考以下示例:

import { HttpClient } from '@angular/common/http';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';

@Component({
  selector: 'app-flav4',
  templateUrl: './flav4.component.html',
  styleUrls: ['./flav4.component.css']
})
export class Flav4Component implements OnInit, OnDestroy {
  orderStatus!: any;
  data!: Observable<any>;
  constructor(private httpcli: HttpClient) { }
  ngOnInit(): void {
    this.data = this.httpcli.get("http://localhost:9898/api/orderStart", { headers: { "content-type": "application/json" } }).pipe(
      mergeMap(orderStart => {
        console.warn(orderStart);
        this.orderStatus = orderStart;
        return this.httpcli.get("http://localhost:9898/api/orderWait", { headers: { "content-type": "application/json" } });
      }),
      mergeMap(orderWait => {
        console.warn(orderWait);
        this.orderStatus = orderWait;
        return this.httpcli.get("http://localhost:9898/api/orderComplete", { headers: { "content-type": "application/json" } });
      })
    );
    this.data.subscribe(orderComplete => {
      console.warn(orderComplete);
      this.orderStatus = orderComplete;
    });
  }
  ngOnDestroy(): void {
  }
}

这是您提供的代码的中文翻译。

英文:

This is normal behaviour as HTTP requests are asynchronous.

You can try chaining them with mergeMap also known as flatmap from RxJS library. I suggest you take a look at the behaviours as well.

Something like this as reference

import { HttpClient } from &#39;@angular/common/http&#39;;
import { Component, OnDestroy, OnInit } from &#39;@angular/core&#39;;
import { Observable } from &#39;rxjs&#39;;
import { mergeMap } from &#39;rxjs/operators&#39;;

@Component({
  selector: &#39;app-flav4&#39;,
  templateUrl: &#39;./flav4.component.html&#39;,
  styleUrls: [&#39;./flav4.component.css&#39;]
})
export class Flav4Component implements OnInit, OnDestroy {
  orderStatus!: any;
  data!: Observable&lt;any&gt;;
  constructor(private httpcli: HttpClient) { }
  ngOnInit(): void {
    this.data = this.httpcli.get(&quot;http://localhost:9898/api/orderStart&quot;, { headers: { &quot;content-type&quot;: &quot;application/json&quot; } }).pipe(
      mergeMap(orderStart =&gt; {
        console.warn(orderStart);
        this.orderStatus = orderStart;
        return this.httpcli.get(&quot;http://localhost:9898/api/orderWait&quot;, { headers: { &quot;content-type&quot;: &quot;application/json&quot; } });
      }),
      mergeMap(orderWait =&gt; {
        console.warn(orderWait);
        this.orderStatus = orderWait;
        return this.httpcli.get(&quot;http://localhost:9898/api/orderComplete&quot;, { headers: { &quot;content-type&quot;: &quot;application/json&quot; } });
      })
    );
    this.data.subscribe(orderComplete =&gt; {
      console.warn(orderComplete);
      this.orderStatus = orderComplete;
    });
  }
  ngOnDestroy(): void {
  }
}

huangapple
  • 本文由 发表于 2023年2月18日 02:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定