Rxjs正在获取AjaxObservable而不是ajax响应

时间:2017-01-27 19:18:51

标签: rxjs observable rxjs5 rxjs-dom

我有下一个Observable我试图过滤以使用户进入网络,但.mapTo(phone => verifyPhoneInNetwork(phone,country))返回AjaxObservable而不是ajax响应

function verifyInNetwork(contacts: any, country: string) {
  const inNetworkOb = Observable
    .from(contacts)
    .map(contact => contact.phones)
    .map(phone => verifyPhoneInNetwork(phone, country))
    .first(({response}) => {
      return !response.invalid && !response.exists;
    })
    .isEmpty()
    .filter(empty => empty);

1 个答案:

答案 0 :(得分:2)

如果verifyPhoneInNetowrk返回Observable,您应该使用switchMap,如下所示:

function verifyInNetwork(contacts: any, country: string) {
  const inNetworkOb = Observable
    .from(contacts)
    .map(contact => contact.phones)
    .switchMap(phone => verifyPhoneInNetwork(phone, country))
    .first(({response}) => {
      return !response.invalid && !response.exists;
    })
    .isEmpty()
    .filter(empty => empty);

详细了解switchMap