How to eliminate these excessive "callbacks" when using Names and subscribes in the TS?

Asked

Viewed 25 times

0

I am working with Ionic 2 and I need to make a request in Storage to redeem a previously saved token, after making this request I need to send this token to the API to be validated. The problem is I’m polluting my code with Promise and subscribe that is generating this cascade exemplified below:

public check(): Promise<boolean> {
    return new Promise(resolve => {
        this.storage.get('token').then((token) => {
            this.network.onConnect().subscribe(() => {
                if(this.network.type != 'none' && this.network.type != 'unknown') {
                    this.http.post('localhost/auth', {
                        token: token
                    }).map(response => response.json()).subscribe(data => {
                        resolve(data.status);
                    });
                }
            });
        });
    });
}

...

check().then(status => {
    if(!status)
        ...
});

Is there a more elegant solution to this case?

  • That’s Angularjs or Angular2?

  • I’m using Angular 2

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.