Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull value out of promise

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

like image 874
john samcock Avatar asked Aug 01 '17 03:08

john samcock


People also ask

Is it possible to force cancel a Promise?

No. We can't do that yet. ES6 promises do not support cancellation yet.

How do you handle Promise rejection?

If an error condition arises inside a promise, you “reject” the promise by calling the reject() function with an error. To handle a promise rejection, you pass a callback to the catch() function. This is a simple example, so catching the rejection is trivial.

What happens if you never resolve a Promise?

So failing to resolve or reject a promise just fails to ever change the state from "pending" to anything else. This doesn't cause any fundamental problem in Javascript because a promise is just a regular Javascript object.


1 Answers

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).

like image 184
jfriend00 Avatar answered Sep 30 '22 17:09

jfriend00