# How to get the parameter value outside of promise

**URL:** <https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435>\
**Category:** ionic-v3\
**Created:** [February 25, 2019, 9:55am UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435 "2019-02-25T09:55:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Velmurugan](https://avatars.discourse-cdn.com/v4/letter/v/74df32/32.png) [@Velmurugan](https://forum.ionicframework.com/u/Velmurugan)\
**Post date:** [February 25, 2019, 9:55am UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435/1 "2019-02-25T09:55:37Z")

</div>

Hi ,

I am having the below code…

my Logged\_user value is ‘9715328568’ which is taking from the local storage

```
if (this.logged_user ==undefined){                                        
  this.storage.get('username').then((auth) => {
    this.logged_user=auth;  
    console.log(this.logged_user,' is already logged in '); //---- result '9715328568,' is already logged in '
  });
  
  console.log(this.logged_user,' is already logged in signup screen'); //-- result undefined is already logged in signup screen
  
}else{
  this.logged_user=this.logged_user
}

```

Outside of promise its shwing as indefined …  
“console.log(this.logged\_user,’ is already logged in signup screen’); //-- result undefined is already logged in signup screen”

---

<div class="post-metadata">

**Author:** ![Dovel](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dovel/32/127722_2.png) [@Dovel](https://forum.ionicframework.com/u/Dovel)\
**Post date:** [February 25, 2019, 10:24am UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435/2 "2019-02-25T10:24:23Z")

</div>

Promise is an asynchronous method. In your case you call `this.storage.get` which returns a Promise. This means the value is only available when the Promise has fulfilled.  
Your console.log() after the `then` method the Promise shows a value of this.logged\_user which is called after the Promise is fulfilled. The value `this.logged_user` is `undefined` because the Promise is not completed.  
The value is only available in the then method of the Promise. All further handling of `this.logged_user` depends on the Promise resolved value.

If you want a clean sync-like code with Promises, I recommend to async-await.

```auto
class YourClass {
  public logged_user: string;

  public async login() {
    if (this.logged_user !== undefined) {
      console.log('User is already set.');
      return;
    }

    console.log('logged_user is ', this.logged_user);
    // Expected Result: Undefined (ok)

    // The value of the resolved Promise is set to this.logged_user.
    // It waits until the Promise is fulfilled. (Does not block the rest of your code. Don't worry.)
    this.logged_user = await this.storage.get('username');

    console.log(this.logged_user, ' is already logged in ');
    // Result: '9715328568,' is already logged in.

    console.log(this.logged_user, ' is already logged in signup screen');
    // Result: '9715328568' is already logged in signup screen.
  }
}

```

Look out for `async` before the method / function name and `await` before Promise. Note: Your method `login` is now a Promise, too.

> **[async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)**
>
> The async function declaration defines an asynchronous function, which returns an AsyncFunction object.

---

<div class="post-metadata">

**Author:** ![Velmurugan](https://avatars.discourse-cdn.com/v4/letter/v/74df32/32.png) [@Velmurugan](https://forum.ionicframework.com/u/Velmurugan)\
**Post date:** [February 25, 2019, 2:28pm UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435/3 "2019-02-25T14:28:14Z")

</div>

Its working fine… Thanks a lot. Dovel

---

<div class="post-metadata">

**Author:** ![Dovel](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dovel/32/127722_2.png) [@Dovel](https://forum.ionicframework.com/u/Dovel)\
**Post date:** [February 26, 2019, 7:35am UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435/4 "2019-02-26T07:35:42Z")

</div>

Great! 🙂 Leave a heart and mark as solved if this is your solution.

---

<div class="post-metadata">

**Author:** ![Velmurugan](https://avatars.discourse-cdn.com/v4/letter/v/74df32/32.png) [@Velmurugan](https://forum.ionicframework.com/u/Velmurugan)\
**Post date:** [February 26, 2019, 9:12am UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435/5 "2019-02-26T09:12:25Z")

</div>

Done… I am new in ionic .I will do the same in next time.🙂

---

<div class="post-metadata">

**Author:** ![Dovel](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dovel/32/127722_2.png) [@Dovel](https://forum.ionicframework.com/u/Dovel)\
**Post date:** [February 26, 2019, 9:33am UTC](https://forum.ionicframework.com/t/how-to-get-the-parameter-value-outside-of-promise/157435/6 "2019-02-26T09:33:34Z")

</div>

Hehe, all’s fine. 😃
