订阅完成后使用Rxjs获取价值

时间:2018-07-19 07:58:56

标签: angular typescript rxjs

当前,我正在使用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);

2 个答案:

答案 0 :(得分:1)

更新的变量将在订阅方法中可用:

 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);
    });

答案 1 :(得分:0)

我解决了我的问题:)

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);
  }
});