从承诺内部返回已解决的观察 [英] Return Resolved Observable from Inside of a Promise

查看:107
本文介绍了从承诺内部返回已解决的观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过扩展默认值来构建自定义Angular 2 http请求,并且我正在使用Ionic 2本地存储来存储身份验证令牌。 (将来可能会使用文件系统)。我的问题是如何从我的http服务返回已解决的promise,以便我可以在我的组件中订阅Observable。我已经尝试过Observable.fromPromise和其他变种无济于事。

I'm trying to build a custom Angular 2 http request by extending the default and I'm using Ionic 2 local storage to store the auth token. (Will likely use file system in future). My issue is how to return a resolved promise from my http service so I can subscribe to the Observable within my component. I've tried Observable.fromPromise and other variations to no avail.

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {

  // Get the token used for this request.
  // * Need to return this promise resolved.
  var promise = this.storage.get('token').then(token => {

    if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
      if (!options) {
        // let's make option object
        options = {headers: new Headers()};
      }
      options.headers.set('Authorization', 'Basic ' + token);
    } else {
    // we have to add the token to the url object
      url.headers.set('Authorization', 'Basic ' + token);
    }

    return super.request(url, options).catch(this.catchAuthError(this));

  }).catch(error => {
    console.log(error);
  });

}

想法是基于这篇博文,但是离子存储返回一个承诺。 http://www.adonespitogo.com/articles/angular-2 -extending-http-provider /

Idea is based on this blog post, but Ionic storage returns a promise. http://www.adonespitogo.com/articles/angular-2-extending-http-provider/

推荐答案

我不知道该存储是否返回兼容的承诺使用Rx,但如果是,则解决方案应如下所示:

I don't know if that storage returns a promise which is compatible with Rx, but if it is then the solution should look like this:

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {

    return Observable
        .fromPromise(this.storage.get('token'))
        .flatMap(token => {

            if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
                if (!options) {
                    // let's make option object
                    options = {headers: new Headers()};
                }
                options.headers.set('Authorization', 'Basic ' + token);
            } else {
                // we have to add the token to the url object
                url.headers.set('Authorization', 'Basic ' + token);
            }

            return super.request(url, options).catch(this.catchAuthError(this));

        });
    });

}

如果承诺与可观察量不兼容,那么仍有办法这样做,虽然不是那么优雅:

If promise is not compatible with observables there's still a way to do that, though it's not that elegant:

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {

    return Observable.create((observer: Observer) => {

        this.storage.get('token').then(token => {

            if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
                if (!options) {
                    // let's make option object
                    options = {headers: new Headers()};
                }
                options.headers.set('Authorization', 'Basic ' + token);
            } else {
                // we have to add the token to the url object
                url.headers.set('Authorization', 'Basic ' + token);
            }

            super.request(url, options).catch(this.catchAuthError(this)).subscribe(result => {
                observer.next(result);
                observer.complete();
            });

        }).catch(error => {
            observer.error(error);
        });

    });

}

这篇关于从承诺内部返回已解决的观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆