如何从javascript承诺中获取价值?

编程入门 行业动态 更新时间:2024-10-24 14:21:41
本文介绍了如何从javascript承诺中获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个具有以下值的 userBalance 承诺对象:

I have a userBalance promise object with following values:

> userBalance Promise { '100000000000000000', domain: Domain { domain: null, _events: { removeListener: [Function: updateExceptionCapture], newListener: [Function: updateExceptionCapture], error: [Function: debugDomainError] }, _eventsCount: 3, _maxListeners: undefined, members: [] } }

问题是,我无法从中获得 100000000000000000.有办法吗?

the problem is, i cannot get that 100000000000000000 from it. is there a way to do so?

推荐答案

唯一获取 promise 的解析值的方法(即使它已经解析)是使用 promise,通过 then 或 await:

The only way to get the resolution value of a promise (even if it's already resolved) is to consume the promise, via then or await:

userBalance.then(value => { // ...use `value`; note this will be called asynchronously });

或在 async 函数中:

const value = await userBalance; // Note this will be asynchronous

在这两种情况下,加上错误处理,除非您将结果承诺传递给其他可以处理它的东西.

In both cases, plus error handling unless you're passing the resulting promise onto something else that will handle it.

更多推荐

如何从javascript承诺中获取价值?

本文发布于:2023-10-25 08:09:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:价值   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!