Асинхронные вызовы - PullRequest
       2

Асинхронные вызовы

0 голосов
/ 06 сентября 2018

Я пытаюсь вызвать эту функцию в extractContractsView.

public getInsurantOrBeneficiary(content_type: number, object_id: number) {
    if (content_type == 25) { 
      this.http.get(AppSettings.API_ENDPOINT + '/api/v1/artifical_persons' + "/" + object_id + "/", this.options)
        .map((response: Response) => response.json())
        .subscribe((result: any) => {
          this.data_name = result;
          this.name = this.data_name['name'];
          console.log('Artifical persons' + this.name);
          return this.name;
        });
    } else if (content_type == 12) { 
      this.http.get(AppSettings.API_ENDPOINT + '/api/v1/private_persons' + "/" + object_id + "/", this.options)
        .map((response: Response) => response.json())
        .subscribe((result: any) => {
          this.data_private_persons = result;
          this.id_private_persons = this.data_private_persons['passport_id'];
          console.log('id_private_persons:' + this.id_private_persons);
        });

        this.http.get(AppSettings.API_ENDPOINT + '/api/v1/passports' + "/" + this.id_private_persons + "/", this.options)
        .map((response: Response) => response.json())
        .subscribe((result: any) => {
          this.data_name = result;
          this.name = this.data_name['second_name'] + ' ' + this.data_name['first_name'] + ' ' + this.data_name['paternal_name'];
          console.log('Name in passport:' + this.name);
          return this.name;
        });
    } else if (content_type == 43) { 
      this.http.get(AppSettings.API_ENDPOINT + '/api/v1/sole_proprietorship' + "/" + object_id + "/", this.options)
        .map((response: Response) => response.json())
        .subscribe((result: any) => {
          this.data_name = result;
          this.name = this.data_name['name'];
          console.log('sole_proprietorship' + this.name);
          return this.name;
        });
    }
    return this.name;
  }

Проблема в том, что когда я вызываю функцию getInsurantOrBeneficiary, я получаю данные только на первой итерации. Но content_type [i] и object_id [i] всегда разные, и я ожидаю, что функция вернет другое this.name. Я не совсем понимаю работу с асинхронностью, и вполне возможно, что this.http.get не успевает работать, и я получаю ответ только один раз.

 public extractContractsView = (response: Response) => {

    let res = response.json();
    let contracts: ContractsView[] = [];

    for (let i = 0; i < res.length; i++) {

      let insurant = this.getInsurantOrBeneficiary(res[i].insurant_id.content_type, res[i].insurant_id.object_id);
      let beneficiary = this.getInsurantOrBeneficiary(res[i].beneficiary_id.content_type, res[i].beneficiary_id.object_id);

      contracts.push(new ContractsView(
        res[i].id,
        res[i].treaty_number,
        res[i].days_left,
        res[i].days_left_before_the_next_payment,
        res[i].payment_status_KB,
        res[i].security_deposit_no_deposit,
        res[i].currency_conversion_only,
        res[i].currency,
        res[i].contact,
        res[i].additional_information,
        res[i].prolongation,
        res[i].improving_conditions,
        res[i].redundancy,
        res[i].akt_on_KB,
        res[i].payment_status_akt_on_KB,
        insurant,
        beneficiary,
        res[i].insurance_type_id.name,
        res[i].insurer_id.name,
        res[i].executor_id,
        res[i].status,
        res[i].payment,
        res[i].time_of_action.date_start + ' - ' + res[i].time_of_action.date_end,
        res[i].finance,
        res[i].date,
        res[i].subagent,
        res[i].insurance_object,
      ));    
    }
    return contracts;
  }

  public getContracts(): Observable<ContractsView[]> {
    let contracts = this.http.get(this.url, this.options)
      .map(this.extractContractsView)
      .catch(this.handleError);
    return contracts;
  }

Спасибо за любую помощь.

1 Ответ

0 голосов
/ 06 сентября 2018

Вам нужно будет провести рефакторинг вашего API, чтобы получать вызовы, чтобы возвращать наблюдаемые и подписываться на них в функции потребления. Вы делаете два вызова API на строку. Вы можете использовать оператор ForkJoin, чтобы объединить результаты двух вызовов и затем завершить вставку в новый массив. Я сокращаю код. Это должно привести вас в правильном направлении.

    public getInsurantOrBeneficiary(content_type: number, object_id: number): Observable<any> {
        if (content_type == 25) { 
          // Return an observable, do not subscribe here
          return this.http.get(endpoint, options)
            .map((response: Response) => response.json());
        }
    }

А потом подпишитесь и обработайте здесь:

public extractContractsView = (response: Response) => {

    let res = response.json();
    let contracts: ContractsView[] = [];

    for (let i = 0; i < res.length; i++) {

// Use a forkJoin to combine the results of 2 or more observables
forkJoin(
    this.getInsurantOrBeneficiary(),
    this.getInsurantOrBeneficiary()
)
.subscribe((result1: any, result2: any) => {
    contracts.push(new ContractsView(result1, result2))
});
}

Обратите внимание, что цикл завершится до того, как все асинхронные результаты вернутся. Как указано в комментариях, это может быть не лучшим подходом. Однако то, что вы хотите сделать, возможно с использованием этой методологии.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...