Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Component A:
- this.service.getDataA().subscribe(
- data => console.log(data))
- service:
- getDataA() {
- param1
- return this.getData(param1)
- }
- getData() {
- //first without JWT
- let sub = new Subject<Response>();
- this.http.get(param1)
- .subscribe(
- res => sub.next(res)
- err => {
- //401 without JWT so I call authService
- this.authServ.getDataWithToken().subscribe(
- data => sub.next(data)
- )
- return sub.asObservable()
- }
- )
- }
- authService:
- getDataWithToken() {
- let sub = new Subject<Response>()
- this.getToken().subscribe(
- token=> {
- //reachable just once
- this.http.get(url, token
- .
- .
- .
- return sub.asObservable();
- }
- getToken(url) {
- let sub = new Subject<string>();
- if (this.tokens[url])
- // with console.log reachable
- sub.next(this.tokens[url])
- else {
- this.http.get(newToken).subscribe(
- (data) => sub.next(data)
- )
- return sub.asObservable()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement