ionic framework - Angular Promise : unable to get value -
i'm coming python world, , beginner in angular / ts / js way of programming.
the ionic storage.get()
method returns promise
. how should value of token in order send http query ?
for now, i'm trying faulty bloc :
get(url: string): observable<response> { let token = ""; this.storage.get('token') .then(token => token = token); return this.http.get(url, { "authorization" : token}); }
of course, request leaves token = "" instead of 1 should have come after promise resolution.
i can see not other method in promise then()
, catch()
. have considered following code :
get(url: string): observable<response> { return this.storage.get('token').then( token => { return this.http.get(url, { "authorization": token } ) ); });
but then, get(url)
returns promise<observable<response>>
instead of observable<response>
, not me much.
if anwser lies within use of defer ($q https://docs.angularjs.org/api/ng/service/$q) , please tell me how class can imported angular / ionic ?
thank assistance :)
no, $q
used in angularjs deal promises. here in angular using observable
mostly.
you use observable.frompromise
convert promise observable followed flatmap
, return observable of response
type.
import { observable } 'rxjs/observable'; import 'rxjs/add/observable/frompromise'; get(url: string): observable<response> { let token = ""; return observable.frompromise(this.storage.get('token')) .flatmap(token => this.http.get(url, { "authorization" : token})); }
Comments
Post a Comment