从 Promise 内部返回已解决的 Observable [英] Return Resolved Observable from Inside of a Promise
问题描述
我正在尝试通过扩展默认值来构建自定义 Angular 2 http 请求,并且我正在使用 Ionic 2 本地存储来存储身份验证令牌.(将来可能会使用文件系统).我的问题是如何从我的 http 服务返回已解决的承诺,以便我可以订阅组件中的 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-扩展-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 兼容的 Promise,但如果是,那么解决方案应该如下所示:
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));
});
});
}
如果 promise 与 observables 不兼容,仍然有办法做到这一点,尽管它不是那么优雅:
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);
});
});
}
这篇关于从 Promise 内部返回已解决的 Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!