[英]Why am I seeing “Promise { <pending> }” from this code on repl.it?
I know this question has been asked a lot on stackoverflow and I've searched a lot and still am not able to understand. 我知道这个问题已经在stackoverflow问了很多,我搜索了很多但仍然无法理解。
async function testFunc() { var test = await getSomething(); //test.resolve(); console.log("hello" + test); return ""; } testFunc().then(token => {}).catch(x => {}); function getSomething() { return "ex"; }
In most answers it is suggested to use .then() to resolve the promise, but I've done that and I still get promise pending. 在大多数答案中,建议使用.then()来解决这个承诺,但我已经做到了,我仍然有待许可。 What is wrong in this ?
这有什么问题?
Tested this on https://repl.it/repls/UntrueLankySorting 在https://repl.it/repls/UntrueLankySorting上测试过
It shows me this: 它告诉我这个:
helloex => Promise { <pending> }
Tested this on https://repl.it/repls/UntrueLankySorting
在https://repl.it/repls/UntrueLankySorting上测试过
It's showing you the result of the call to catch
here: 它显示了
catch
调用的结果:
testFunc().then(token => {}).catch(x => {});
You don't care about that promise, it's just that that environment shows it to you. 你不关心这个承诺,只是那个环境向你展示。 It is indeed a pending promise as of when
catch
returns, but it gets settled later. 它确实是一个待定的承诺,当
catch
返回时,但它会在稍后解决。
That's just an aspect of the environment you were running it in. The code itself is fine other than the issue I mentioned in comments (that getSomething
doesn't return a promise, so there's no need to await
it). 这只是你运行它的环境的一个方面。代码本身很好,除了我在评论中提到的问题(
getSomething
没有返回一个promise,因此没有必要await
它)。
In a comment you've asked: 在评论中你问过:
Would
catch
return in this case?会
catch
在这种情况下回报? Since no error is thrown it should resolve atthen
right?由于不会引发错误,应该在解决
then
好吗?
then
and catch
always return a promise. then
catch
总是返回一个承诺。 The promise they return gets resolved or rejected depending on what happens to the promise you called then
/ catch
on and, if their handler gets run, what happens in the handler and what it returns. 他们返回的承诺得到解决或拒绝取决于发生了什么你叫许
then
/ catch
上,如果他们的处理程序被运行时,会发生什么情况的处理程序和它返回什么。
In this example, here's what that code does: 在这个例子中,这是代码的作用:
testFunc
and gets the promise it returns ( async
functions always return promises). testFunc
并获取它返回的promise( async
函数总是返回promises)。 Call that Promise A. then
on Promise A. then
returns a new promise (Promise B). then
调用Promise A. then
返回一个新的承诺(Promise B)。 catch
on Promise B. catch
returns a new promise (Promise C). catch
Promise B. catch
返回一个新的承诺(Promise C)。 repl.it
shows you Promise C, which is pending at that point.¹ repl.it
显示Promise C,此时正在等待.¹ then
handler. then
处理程序。 The then
handler (effectively) returns undefined
. then
handler(有效地)返回undefined
。 undefined
, which fulfills Promise C (without calling the catch
handler, because the promise was fulfilled, not rejected). undefined
,它实现了Promise C(没有调用catch
处理程序,因为promise已经完成,但没有被拒绝)。 ¹ It would be permissible for an implementation to show a fulfilled state there instead in this example , but for all intents and purposes, it's best to think of the promise as pending at that point. ¹ 在本例中 ,允许实现在那里显示满足状态,但是对于所有意图和目的,最好将该承诺视为当前未决。 Since code can never directly observe the state of a promise, your code can't tell the difference.
由于代码永远不能直接观察到promise的状态,因此您的代码无法区分。
² Similarly, it would be permissible if it were already fulfilled before then
was called on it, but similarly you can never see that directly in code, so... ²同样,这将是允许的,如果它是之前就已经完成
then
叫上,但同样你永远无法看到直接在代码中,所以...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.