简体   繁体   中英

Return a value from a promise in angular4 (Ionic 3)

I have faced a deadlock in an issue while developing in Ionic 3 (angular4). I am simply redoing a formerly written angular app in Ionic but the issue started ever since I decided to use the Ionic "storage" instead of the "localStorage".

The issue is caused by a tiny difference, in local storage the "get" methd simply returns a value, but Ionic's storage "get" method returns a promise.

Now, I need to have a function that reads a specific value from the storage and returns it, and for some reasons I cannot accommodate a promise where I want to use this value.

Is there a way to get a similar behaviour to the localStorage on get method?

getAccessToken()
  {
    return this._storage.get('bdAccessToken');
  }

getAuthorizationOptions(){
                let token = this._authService.getAccessToken();
                let header = new Headers({
                    'Authorization': 'Bearer '+ token
                });
                let ro = new RequestOptions({
                    headers: header
                });
                let options = new RequestOptions();
                if (options.headers) options.headers.delete("Authorization");
                options.headers = header;
                return options;
}



get(url:string, options?:RequestOptions):Observable<Response>
    {    

        return super.get(url, this.getAuthorizedOptions())
            .catch(err => {
                console.log("shit 1");
                console.log(err);
                if (err && err.status === 401){
                    console.log("401 here...");
                    return this._authService.refreshToken()
                        .flatMap(r =>
                            super.get(url, this.getAuthorizedOptions())
                        )
                        .catch(err2 => {
                            this.redirect();
                            return Observable.throw(err2);
                        });
                }
                else {
                    return Observable.throw(err);
                }
            });
}

You cannot use an asynchronous interface as if it were synchronous. That's fundamental. You have to rewrite the part of your code using that interface to operate asynchronously as well.

The simplest way to do that, if you have access to async/await , is as

// OLD
function getStuff() {
  const stuff = get('stuff');
  return stuff + 1;
}

// NEW
async function getStuff() {
  const stuff = await getAsync('stuff');
  return stuff + 1;
}

Of course, getStuff is now asynchronous, so it is possible that the code using it will also require changes.

for some reasons I cannot accommodate a promise where I want to use this value.

I can't imagine what those reasons would be. Any code of the form

const value = getValue();
doSomethingWithValue(value);

can always be rewritten as

const valueP = getPromiseForValue();
valueP.then(value => doSomethingWithValue(value));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM