本文介绍了如何从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承诺中获取价值?
发布评论