6

I have this code below: How can i pull the value out of this promise?

  var value = Auth.loggedinUser().then(function (user){
    return user.id
  });

  console.log(value)

This is my promise below:

Promise {$$state: Object}
    $$state:Object
    status:1
    value:2
__proto__:Object
__proto__:Object

I am trying to just get the value out and set it to my value variable. Which should be 2 or whatever it would return.

My goal is to set the value of the promise to a global variable if possible

john samcock
  • 137
  • 1
  • 8
  • 2
    Promise `.then` method is how ... note, the return value of `.then` is a Promise, only inside the `.then` callback are you able to get the promised value (once it is resolved) – Jaromanda X Aug 01 '17 at 03:50
  • Can i set that to a global variable outside of it? – john samcock Aug 01 '17 at 03:52
  • 3
    yes, but it'll still be asynchronously set, so no guarantees of when the value will be there ... so, no reason to use a global for a future value. Note: Asynchronous code is bewildering until you understand it – Jaromanda X Aug 01 '17 at 03:53
  • 3
    Globals are a no-no. You are not grasping ```Promises``` correctly – Wainage Aug 01 '17 at 03:53
  • 2
    @Wainage - I would venture he's not grasping asynchronous code in general :p – Jaromanda X Aug 01 '17 at 03:54
  • @johnsamcock Do the work you need inside the ```.then``` function – Wainage Aug 01 '17 at 03:55
  • Possible duplicate of [How to access the value of a promise?](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise) – Herohtar May 27 '19 at 03:59

1 Answers1

9

The whole concept of a promise is that you obtain and use the value of a promise only in the .then() handler or with await. You cannot fetch the value directly from the promise and should not be trying to because if you are, it is likely because you don't actually grasp how asynchronous code works and how promises work and how the timing of when the value will be available is unknown and thus you have to use the promise notification system to know when the value is available.

So, in your code, you need to consume the user.id inside the .then() handler or in some function that you pass it to that is called from inside the .then() handler.

Auth.currentUser().then(function (user){
    // use user.id here
    // or call some other function here and pass it the id myFunc(user.id)
});

// You do not use user.id here

The code that fetches the user object is asynchronous. That means that it is non-blocking and that it completes some unknown time in the future. The ONLY place you can possibly know that the user or user.id value is now valid is INSIDE the .then() handler.

Here's an analogy. You take your car into service. They say they have no idea when it will be ready for pickup because they don't yet know what's wrong and how long it will take them to diagnose, get parts and to fix it. They say they will call you when its done. So, at this point, you can't make any plans that include using your car because you literally don't know when it will be ready. The service center has given you a "promise" that they will call you when it's ready and then (and only then) can you come in to pick it up or make plans about when you will have your car. So, the only places you can make plans for your car are situations that come after they've called you back and told you when it would be ready. That callback from the service center is like the promise's .then() handler. Only in code that stems from that .then() handler can you do anything with the resolved value.

Promises are purposely opaque so you cannot look inside of them. They've defined a standard interface for being notified of completion with .then() or error with .catch(). That's how you use promises. Asynchronous coding in general takes some getting used to (a different paradigm than used in most other programming environments) and promises are built on top of that to give asynchronous operations a standard way of notifying, coordinating, chaining and propagating errors that also takes a bit of learning.

My goal is to set the value of the promise to a global variable if possible

This is pretty much never the correct way to use a value obtained via an asynchronous operation for several reasons. Besides the usual reasons to avoid global variables, any code that might want to use this value simply never have any idea when it's actually a valid value. That info is only known inside the .then() handler or after using await (or in case of errors in a .catch() handler).

jfriend00
  • 683,504
  • 96
  • 985
  • 979