retryWhen从Angular 5 httpClient可观察

时间:2018-07-16 07:51:38

标签: angular typescript error-handling rxjs angular-httpclient

如何将以下内容作为Angular 5 httpClient获取的一部分,即:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define true 1
#define false 0

float calculate_percent( float score, float max_score );
float getAvg( float num1, float num2, float num3 );
char getGradeLetter( float score );
void print_line( char symbol, int count );

int main()
{
    char name[20] = { 0 };
    int scores[3] = { 0 };
    float avg = 0;
    int reply;

    print_line( '*', 10 );

    while ( true )
    {
        print_line( '*', 10 );

        printf( "\nChose an option.\n" );
        printf( "1.Enter user name.\n2.Enter exam scores.\n3.Display average exam scores.\n4.Display Summary.\n5.Quit\n" );
        scanf( "%d", &reply );

        switch ( reply )
        {
            case 1:
                printf( "Enter name.\n" );
                scanf( "%s", &name[20] );
                break;

            case 2:
                for ( int i = 0; i < 3; i++ )
                {
                    printf( "Enter a score.\n" );
                    scanf( "%d", &scores[i] );
                }
                break;

            case 3:
                if ( name[20] == 0 && scores[0] == 0 || scores[1] == 0 || scores[2] == 0 )
                {
                    printf( "You havent entered the scores.\n" );
                }
                else
                {
                    avg = getAvg( scores[0], scores[1], scores[2] );
                    printf( "Your average is %.2f\n", avg );
                }
                break;

            case 4:
                if ( name[20] == 0 && scores[0] == 0 || scores[1] == 0 || scores[2] == 0 )
                {
                    printf( "Name and scores are empty.\n" );
                }
                else
                {
                    printf( "Hello %s, your scores are %d %d %d.\n", name[20], scores[0], scores[1], scores[2] );
                    printf( "your average is %d with a letter of grade %c.\n", avg, getGradeLetter( avg ) );
                }
                break;

            case 5:
                exit( 0 );
        }
    }

    getchar();
    return 0;
}

float calculate_percent( float score, float max_score )
{
    return score / max_score * 100;
}

float getAvg( float num1, float num2, float num3 )
{
    return ( num1 + num2 + num3 ) / 3;
}

char getGradeLetter( float score )
{
    char grade_letter;

    if ( score >= 90 )
    {
        grade_letter = 'A';
    }
    else if ( score >= 80 )
    {
        grade_letter = 'B';
    }
    else if ( score >= 70 )
    {
        grade_letter = 'C';
    }
    else if ( score >= 60 )
    {
        grade_letter = 'D';
    }
    else if ( score > 0 && score < 59 )
    {
        printf( "you failed the test.\n\n" );
    }

    return grade_letter;
}

void print_line( char symbol, int count )
{
    for ( int i = 0; i < count; i++ )
    {
        printf( "%c", symbol );
    }
}

我有以下可观察的东西:

.retryWhen((error) => error.delay(5000).take(3))

我不确定该怎么做,就是增加重试功能,当上述httpClient收到呼叫时,即5秒钟后,请再次尝试该过程3次。

已更新

我现在尝试添加retryWhen,但是由于我的.concat无法正常工作并告诉我它不是函数,因此我不确定如何将错误抛出。

请注意,我已导入以下内容:

getMyURL(): Observable<any> {
    const url = `${this.URL}`;
    return this.httpClient
            .get(url)
            .retryWhen(errors =>                     
               errors.delay(5000)
                .take(3)
                .concat(Observable.throw(new Error('Retry limit exceeded!')))
            );
            //.catch((err: HttpErrorResponse) => {
            //  return ErrorObservable.create(err);
            //});
}

如果经过3次尝试,.get均未成功,我想向我的订阅抛出一个错误,即:

import 'rxjs/add/operator/retryWhen';
import 'rxjs/add/operator/delay';
import 'rxjs/add/operator/take';
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/concat';

我还希望发现如何添加.getMyURL().subscribe( res => { // response code ..... }, error => { this.errors = error; console.log("----->>> subscribe error",this.errors); } ); .retryWhen来进行错误处理。

0 个答案:

没有答案