如何继续致电后端API,直到我在回复中收到200个好的

发布于 2025-02-11 04:29:32 字数 364 浏览 2 评论 0原文

我正在开发一个Angular项目,我需要致电后端API,如果它不是返回200,那么我需要每30秒钟调用此API,直到我获得200个AP。

我使用的是Angular,我通常称为后端API的格式是:

this.productServices.onProductBuy(product_id).subscribe(response => {
    console.log(response)
});

这只是一个get请求。

OnProductBuy()包含一些调用API代码,如果未收到此onProdCutBuy API,则仅收到http 200, 我需要再一次称此API,直到获得200。

I am developing an angular project, I need to call a backend api, if it's not return 200, then i need to call this api every 30 seconds, until I get 200 OK.

I am using angular, the format I usually call an backend api is this:

this.productServices.onProductBuy(product_id).subscribe(response => {
    console.log(response)
});

onProductBuy() contains some calling api code, it's just simply a get request

If a HTTP 200 is not received for this onProdcutBuy api, I need to call this api one more time until I get the 200.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各空 2025-02-18 04:29:32

这应该解决问题:

this.productServices.onProductBuy(product_id).pipe(
  retry({
    delay: 30 * 1000
  } as any)
).subscribe(response => {
  console.log(response)
});

这重试了无限的次数,也增加了30秒的延迟。 重试的类型定义似乎与我使用的版本打破了。这就是为什么我必须将用作任何

但是 source code> source Code 表明,该<显示了<代码>重试接受具有延迟属性的对象:

export interface RetryConfig {
  /**
   * The maximum number of times to retry. If `count` is omitted, `retry` will try to
   * resubscribe on errors infinite number of times.
   */
  count?: number;
  /**
   * The number of milliseconds to delay before retrying, OR a function to
   * return a notifier for delaying. If a function is given, that function should
   * return a notifier that, when it emits will retry the source. If the notifier
   * completes _without_ emitting, the resulting observable will complete without error,
   * if the notifier errors, the error will be pushed to the result.
   */
  delay?: number | ((error: any, retryCount: number) => ObservableInput<any>);
  /**
   * Whether or not to reset the retry counter when the retried subscription
   * emits its first value.
   */
  resetOnSuccess?: boolean;
}

export function retry<T>(count?: number): MonoTypeOperatorFunction<T>;
export function retry<T>(config: RetryConfig): MonoTypeOperatorFunction<T>;

This should do the trick:

this.productServices.onProductBuy(product_id).pipe(
  retry({
    delay: 30 * 1000
  } as any)
).subscribe(response => {
  console.log(response)
});

This retry an infinite amount of times and also add a delay of 30 second. The type definition of retry seems to be broken with the version I am using. That's why I had to use as any.

But the source code shows, that retry accepts an object with a delay property:

export interface RetryConfig {
  /**
   * The maximum number of times to retry. If `count` is omitted, `retry` will try to
   * resubscribe on errors infinite number of times.
   */
  count?: number;
  /**
   * The number of milliseconds to delay before retrying, OR a function to
   * return a notifier for delaying. If a function is given, that function should
   * return a notifier that, when it emits will retry the source. If the notifier
   * completes _without_ emitting, the resulting observable will complete without error,
   * if the notifier errors, the error will be pushed to the result.
   */
  delay?: number | ((error: any, retryCount: number) => ObservableInput<any>);
  /**
   * Whether or not to reset the retry counter when the retried subscription
   * emits its first value.
   */
  resetOnSuccess?: boolean;
}

export function retry<T>(count?: number): MonoTypeOperatorFunction<T>;
export function retry<T>(config: RetryConfig): MonoTypeOperatorFunction<T>;
草莓味的萝莉 2025-02-18 04:29:32

您可以使用 rxjs retry

this.productServices.onProductBuy(product_id)
  .pipe(retry(2)) // <-- Here
  .subscribe(response => {
    console.log(response)
  });

随着上述内容,它将重试两次。如果您想永远重试,请不要添加一个数字,它将不断重试直到获得成功为止:

this.productServices.onProductBuy(product_id)
  .pipe(retry()) // <-- Here
  .subscribe(response => {
    console.log(response)
  });

不要忘记避免内存泄漏陷阱带有订阅。

You could use rxjs retry:

this.productServices.onProductBuy(product_id)
  .pipe(retry(2)) // <-- Here
  .subscribe(response => {
    console.log(response)
  });

With the above it will retry twice. If you'd like to retry forever, just don't add a number to it, it will keep retrying until a success is received:

this.productServices.onProductBuy(product_id)
  .pipe(retry()) // <-- Here
  .subscribe(response => {
    console.log(response)
  });

Don't forget to avoid the memory leak trap with subscriptions.

猫瑾少女 2025-02-18 04:29:32

好吧!

因此,就我个人而言,我相信这应该由后端框架来处理。但是,如果您真的想在应用程序的角度上实现这一目标,这是我对问题的解决方案(显然不使用重试,因为有时可能会像其他提到的那样有问题):

  1. 将产品服务类中的主题定义为下图:

    public onproductBysubject = new object();

  2. 在component.ts文件中定义一种方法(如果还没有定义),以执行服务类中所需方法的调用:

    public justanexample(){
    this.productservices.onproductbuy(product_id).subscribe(response =&gt; {

    if(wendesp.code!= 200){
    settimeout(this.productservices.onproductbysubject.next(true),30000);
    }
    console.log(响应)
    });
    }

  3. 在ngoninit()生命周期钩功能中,进行订阅:

    this.mysubscription = this.productservices.onproductbysubject.subscribe(value =&gt; {{
    if(value)this.justanexample();
    });

  4. 在ngdestroy()生命周期钩功能中,退订“ mySubscription”。

让我知道这是否会有所帮助并为我的答案提供帮助,以便对他人有所帮助。

Alright!

So personally, I believe this should be handled by the backend framework. However if you really want to achieve this on the angular side of the application, here is my solution of the problem ( Obviously without using retry as that can sometime be problematic as other mentioned already ) :

  1. Define a Subject in ProductService Class as below :

    public onProductBySubject = new Subject();

  2. Define a method in component.ts file ( if not defined already ) to perform the invocation of the required method from the service class :

    public justAnExample() {
    this.productServices.onProductBuy(product_id).subscribe(response => {

    if(response.code != 200){
    setTimeOut(this.productServices.onProductBySubject.next(true),30000);
    }
    console.log(response)
    });
    }

  3. In the ngOnInit() life cycle hook function , make a subscription :

    this.mysubscription = this.productServices.onProductBySubject.subscribe(value=>{
    if(value) this.justAnExample();
    });

  4. in ngDestroy() life cycle hook function, unsubscribe 'mysubscription'.

Let me know if this will help and upvote my answer so that it will help others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文