如何实现承诺的价值 [英] How to pull value out of promise

查看:64
本文介绍了如何实现承诺的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有以下代码:如何从承诺中提取价值?

  var值= Auth.loggedinUser().然后(函数(用户){返回user.id});console.log(值) 

这是我的承诺:

  Promise {$$ state:Object}$$ state:Object状态:1价值:2__proto__:对象__proto__:对象 

我正在尝试获取值并将其设置为我的value变量.应该是2或返回什么.

我的目标是在可能的情况下将promise的值设置为全局变量

解决方案

promise的整个概念是仅在 .then()处理程序中获得并使用promise的值.您不能直接从Promise中获取值,也不应尝试这样做,因为如果这样的话,很可能是因为您实际上并不掌握异步代码的工作方式和Promise的工作方式.

因此,在您的代码中,您需要在 .then()处理程序内或在您将其传递给该函数的某些函数中使用 user.id .then()处理程序内部.

  Auth.currentUser().then(函数(用户){//在这里使用user.id//或在此处调用其他函数,然后将其传递给id myFunc(user.id)});//您在这里不使用user.id 

获取 user 对象的代码是异步的.这意味着它是非阻塞的,并且它将在将来完成一些未知的时间.您可能只知道 user user.id 值现在有效的唯一位置是 .then()处理程序内.

这是一个比喻.您将汽车投入使用.他们说,他们不知道什么时候准备取货,因为他们还不知道出什么问题以及诊断,获取零件和修理需要多长时间.他们说完成后会打电话给您.因此,在这一点上,您无法制定包括使用汽车在内的任何计划,因为您实际上不知道汽车何时准备就绪.服务中心给了您一个承诺",他们会在准备就绪时给您打电话,然后(只有这样)您才能进来取货,或者为您准备何时开车的计划.因此,您唯一可以为自己的汽车制定计划的地方是在他们回电并告诉您准备就绪的时间之后.服务中心的回调类似于promise的 .then()处理程序.只有在源自该 .then()处理程序的代码中,您才能对解析后的值进行任何操作.

故意是不透明的,因此您无法查看它们的内部.他们定义了一个标准接口,用于通过 .then()通知完成或通过 .catch()通知错误.这就是您使用诺言的方式.异步编码通常需要一些时间来适应(与大多数其他编程环境中使用的范式不同),并在此基础上建立承诺,以使异步操作具有通知,协调,链接和传播错误的标准方式,这也需要一点时间学习.

我的目标是在可能的情况下将promise的值设置为全局变量

出于多种原因,这几乎永远不是使用通过异步操作获得的值的正确方法.除了避免使用全局变量的常见原因外,任何可能想要使用该值的代码都根本不会知道何时它实际上是一个有效值.该信息仅在 .then()处理程序内部(或在 .catch()处理程序中发生错误的情况下)是已知的.

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

解决方案

The whole concept of a promise is that you obtain and use the value of a promise only in the .then() handler. 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.

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 in case of errors in a .catch() handler).

这篇关于如何实现承诺的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆