相关疑难解决方法(0)

typescript - 等待一个observable/promise完成,并返回observable

我对Typescript和RxJS很安静,我尝试返回一个observable,而不是其他observable完成:

public myObservable = () : Observable<boolean> => {
    console.log('retrieving the token in DB');
    return Observable.create(observer => {
        setTimeout(() => {
            observer.next(true);
            observer.complete();
        }, 5000);
    });
}

public makeRequest = (): Observable<any> => {
    return this.myObservable().subscribe(
        function (x) {
            console.log('I have the token, now I can make the HTTP call');

            return this.http.get('http://jsonplaceholder.typicode.com/posts/1')
                .map( (responseData) => {
                    return responseData.json();
                })
                .map((item:any) => {
                    return {
                        id: item.id,
                        userId: item.userId,
                        title: item.title,
                        body: item.body
                    };
                });

        },
        function (err) {
            console.error('Error: ' …
Run Code Online (Sandbox Code Playgroud)

observable rxjs typescript

31
推荐指数
2
解决办法
7万
查看次数

Angular - RxJs ForkJoin 如何在出现错误后继续多个请求

除了使用不同的参数外,我多次查询单个 API 端点。无论出于何种原因,其中一些请求可能会失败并返回 500 错误。如果他们这样做,我仍然希望其他请求继续并返回所有成功请求的数据。

let terms = [];
terms.push(this.category.category);
terms = terms.concat(this.category.interests.map((x) => x.category));

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

    const params = {
        term: terms[i],
        mode: 'ByInterest'
    };


    const request = this.evidenceService.get(this.job.job_id, params).map((res) => res.interactions);

    this.requests.push(request);

}

const combined = Observable.forkJoin(this.requests);

combined.subscribe((res) => {
    this.interactions = res;
});
Run Code Online (Sandbox Code Playgroud)

fork-join rxjs angular

9
推荐指数
2
解决办法
6613
查看次数

RxJS Angular2在Observable.forkjoin中处理404

我目前正在链接一堆http请求,但是在订阅之前我无法处理404错误.

我的代码:

在模板中:

...
service.getData().subscribe(
    data => this.items = data,
    err => console.log(err),
    () => console.log("Get data complete")
)
...
Run Code Online (Sandbox Code Playgroud)

在服务中:

...
getDataUsingUrl(url) {
    return http.get(url).map(res => res.json());
}

getData() {
    return getDataUsingUrl(urlWithData).flatMap(res => {
        return Observable.forkJoin(
            // make http request for each element in res
            res.map(
                e => getDataUsingUrl(anotherUrlWithData)
            )
        )
    }).map(res => {
        // 404s from previous forkJoin
        // How can I handle the 404 errors without subscribing?

        // I am looking to make more http requests from …
Run Code Online (Sandbox Code Playgroud)

http fork-join observable rxjs angular

5
推荐指数
1
解决办法
8276
查看次数

有没有办法使用 RxJS 将 n 个 http 请求链接在一起?

在下面的代码中,我有一个User对象,该对象可以具有与之关联的可变数量的帐户 ID。当用户更新时,应为用户对象中包含的每个帐户发出额外请求:

class User {

  id: number;

  accountIds: Array<number>;
}

// Side effects class
@Effect()
update$ = this.actions$
  .ofType('USER_UPDATE')
  .switchMap(action => {

    let user = action.payload;

    for (let accountId of user.accountIds) {
      let account = { id: accountId, userIds: [user.id] };

      // Is there any way to chain these api calls with switch maps
      // and then finally switch map to the user request below?
      return this.apiService.post(`/accounts/${account.id}`, account);
    }

    return this.apiService.post(`/users/${userid}`, user);
  }
Run Code Online (Sandbox Code Playgroud)

有没有办法使用 RxJS 使用switchMap(或类似的东西)将这些调用链接在一起,以便在 …

rxjs angular-http angular

3
推荐指数
1
解决办法
1839
查看次数

如果在角度中调用两次,如何等待相同的observable完成?

我的问题与如何"如何等待观察完成"不一样.我已经使用switchMap在我的代码中解决了这个问题.目前我有一个api get调用,在执行httpget之前,它将检查refreshToken是否已过期,在这种情况下,它将生成一个新令牌并等待它,然后执行httpget,这很棒.

我的问题是,如果我有以下senario:

this.getUsers();
this.getLocations();
Run Code Online (Sandbox Code Playgroud)

让我们来看一下令牌需要刷新的情况(我们需要等待刷新):首先我们调用this.getUsers()这将触发创建一个新令牌,并且在创建令牌后将获得get用户.同时我们也调用this.getLocations(),它也会触发创建一个新的令牌,并且不会在第一次完成时等待.我需要找到一种方法,以便第二次获得等待第一次完成,以便两者将使用相同的新令牌.

rxjs angular

1
推荐指数
1
解决办法
239
查看次数

标签 统计

rxjs ×5

angular ×4

fork-join ×2

observable ×2

angular-http ×1

http ×1

typescript ×1