How to eliminate these excessive callbacks when using promises and subscribes in TS?

0

I'm working with ionic 2 and need to make a request in the storage to redeem a previously saved token, after making this request I need to send this token to the API for it to be validated. The problem is that I am polluting my code with Promise and subscribe that is generating this cascade shown 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 for this case?

    
asked by anonymous 14.02.2018 / 18:37

0 answers