【问题标题】:Use Rxjs to get value when subscribe is finished订阅完成后使用 Rxjs 获取值
【发布时间】:2018-07-19 07:58:55
【问题描述】:

目前,我正在使用 setTimeout 来获取 contactInfoArry。

我想在订阅事件完成时使用 RxJS 获取 'contactInfoArry' 而不是 setTimeout。

因为 'getSummaryAPI' 是一个 API,我需要等待响应。

JS Bin 下面是代码供参考:

JS Bin

   

 const subscribe = getSummaryAPI.subscribe(con => {
      contactInfoArry = contactInfoArry.concat({
        name: con.name,
        profilePhotoUrl: con.profilePhotoUrl,
        type: con.type,
        id: con.id,
        email: con.contactSummary ? con.contactSummary.defaultEmail ? con.contactSummary.defaultEmail.emailAddress : '' : '',
        partnerid: con.contactSummary ? con.contactSummary.partnerContactId ? con.contactSummary.partnerContactId : '' : ''
      });
    });
    
    console.log(contactInfoArry);
    
    setTimeout(() => {
        console.log(contactInfoArry);
    }, 1000);

【问题讨论】:

  • 你在寻找 forkJoin 吗?
  • 我在下面解决了我的问题。我用自己的标志来做。JS Bin

标签: angular typescript rxjs


【解决方案1】:

更新后的变量将在 subscribe 方法中可用:

 const subscribe = getSummaryAPI.subscribe(con => {
      contactInfoArry = contactInfoArry.concat({
        name: con.name,
        profilePhotoUrl: con.profilePhotoUrl,
        type: con.type,
        id: con.id,
        email: con.contactSummary ? con.contactSummary.defaultEmail ? con.contactSummary.defaultEmail.emailAddress : '' : '',
        partnerid: con.contactSummary ? con.contactSummary.partnerContactId ? con.contactSummary.partnerContactId : '' : ''
      });
      console.log(contactInfoArry);
    });

【讨论】:

    【解决方案2】:

    我解决了我的问题:)

    JS Bin

    let contactInfoArry: contactInfo[] = [];
    const contacts = [{
        "id": "001",
        "name": " Happiest  Job",
        "type": "person",
        "gender": null,
        "partnerid": null,
        "profilePhotoUrl": "https://123.jpg",
        "lastModified": "2017-06-30T03:45:57.333362Z",
        "created": "2017-06-30T03:45:57.333362Z",
        "categories": [],
        "isActive": true
      },
      {
        "id": "002",
        "name": " Jane  Williams",
        "type": "person",
        "gender": null,
        "partnerid": null,
        "profilePhotoUrl": "https://456.jpg",
        "lastModified": "2017-06-13T07:44:48.66184Z",
        "created": "2017-06-13T07:44:48.66184Z",
        "categories": [{
          "id": "0a198a57-4522-42de-9be2-4ed6959a9dd7",
          "name": "Good",
          "description": "Level",
          "configuration": "label-primary",
          "lastModified": "2018-03-16T03:19:56.4066687Z",
          "created": "2018-03-16T03:19:56.4066687Z",
          "isActive": true
        }],
        "isActive": true
      },
      {
        "id": "003",
        "name": " Jane  Williams",
        "type": "person",
        "gender": null,
        "partnerid": null,
        "profilePhotoUrl": "",
        "lastModified": "2017-12-18T04:26:51.8433245Z",
        "created": "2017-11-22T00:36:32.001427Z",
        "categories": [],
        "isActive": true
      }
    ];
    
    let getSumIndex = 0;
    let contactsLength = contacts.length;
    
    contacts.map(con => {
      con.contactSummary = {
        "partnerContactId": "789",
        "defaultEmail": {
          "id": "009",
          "emailAddress": "jane@last.com",
          "type": "personal",
          "rank": 0,
          "isDefault": true,
          "lastModified": "2017-08-19T16:26:39.333661Z",
          "created": "2017-06-13T07:44:48.66184Z",
          "isActive": true
        },
        "dateOfBirth": "0001-01-01T00:00:00Z",
        "isDraft": false,
        "lastModified": "2017-06-13T07:44:48.66184Z",
        "created": "2017-06-13T07:44:48.66184Z",
        "isActive": true
      };
      let obj = {
        name: con.name,
        profilePhotoUrl: con.profilePhotoUrl,
        type: con.type,
        id: con.id,
        email: con.contactSummary ? con.contactSummary.defaultEmail ? con.contactSummary.defaultEmail.emailAddress : '' : '',
        partnerid: con.contactSummary ? con.contactSummary.partnerContactId ? con.contactSummary.partnerContactId : '' : ''
      }
      contactInfoArry = contactInfoArry.concat(obj);
      getSumIndex++
      if (getSumIndex == contactsLength) {
        console.log(contactInfoArry);
      }
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2021-06-19
      • 2020-07-13
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多