簡體   English   中英

從承諾內部返回已解決的觀察結果

[英]Return Resolved Observable from Inside of a Promise

我正在嘗試通過擴展默認值來構建自定義Angular 2 http請求,並且我正在使用Ionic 2本地存儲來存儲身份驗證令牌。 (將來可能會使用文件系統)。 我的問題是如何從我的http服務返回已解析的promise,以便我可以在我的組件中訂閱Observable。 我試過Observable.fromPromise和其他變種無濟於事。

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

}

Idea基於這篇博客文章,但Ionic存儲返回了一個承諾。 http://www.adonespitogo.com/articles/angular-2-extending-http-provider/

我不知道該存儲是否返回與Rx兼容的promise,但如果是,則解決方案應如下所示:

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

        });
    });

}

如果promise與observable不兼容,那么還有一種方法可以做到這一點,盡管它並不那么優雅:

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

    });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM