简体   繁体   English

如何解决无限的Javascript承诺?

[英]How do I resolve an infinite Javascript promise?

I'm using the Apollo GraphQL library, and at one point it returns a promise. 我正在使用Apollo GraphQL库,有一次它返回了一个Promise。 I can see that it's a resolved promise when I inspect it in the console: 在控制台中检查它时,我可以看到它是一个已解决的承诺:

Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Object}
__proto__
:
Promise
[[PromiseStatus]]
:
"resolved"
[[PromiseValue]]
:
Object

However, when I Promise.resolve(thatPromise ) I get back ... the same promise. 但是,当我Promise.resolve(thatPromise )时,我得到了……同样的承诺。 No matter how many times I resolve the promise, it keeps returning a promise, making it impossible to access the promise's value. 无论我解决承诺多少次,它都会不断返回承诺,从而无法获取承诺的价值。

I know the value is in there (if I inspect [[PromiseValue]] it's a plain object with values) ... I just can't figure out how to get it out at the code level instead of in the browser console. 我知道值在那里(如果我检查[[PromiseValue]]这是一个带有值的普通对象)...我只是想不出如何在代码级而不是在浏览器控制台中获取它。

Has anyone ever run in to something like this, and if so were you able to figure out how to extract the value of such an infinitely resolving promise? 有没有人碰到过这样的事情,如果是这样的话,您是否能够弄清楚如何提取这种无限解决的诺言的价值?

PS I did try: PS我确实尝试过:

thatPromise['[[PromiseValue]]'];

but it appears to be a special browser value that you can't access from the code. 但它似乎是您无法从代码中访问的特殊浏览器值。

PPS Just realized it may or may not actually be a promise. PPS刚意识到这实际上可能不是一个承诺。 I get it by calling response.clone().json() , so while it looks like a promise maybe it's just a clone of a promise, in which case that might explain why I can't resolve it. 我通过调用response.clone().json()获得它,因此,虽然看起来像一个承诺,但它可能只是承诺的一个克隆,在这种情况下,这也许可以解释为什么我无法解决它。 However, it doesn't explain how I can extract the value. 但是,它没有解释我如何提取值。

EDIT 编辑

I think there's weirdness going on here and I'm not reporting all of the relevant details. 我认为这里很奇怪,我没有报告所有相关细节。 Will post more info as soon as I sort things out better (don't want to send anyone on a wild goose chase). 我会更好地解决问题时将发布更多信息(不想派人追赶鹅)。

There's no reason for you to have to call Promise.resolve to get the value. 您没有理由必须致电Promise.resolve来获取价值。 Remember that Promises are asynchronous. 请记住,承诺是异步的。 You have to access it's value like this: 您必须像这样访问它的值:

myPromise.then(value => {
  console.log(value)
  // do whatever you're going to do with value
};

You can read up more [here][1] on how Promises work. 您可以在此处[1]阅读更多有关Promises工作原理的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM