英文:
javascript await - how does it know the resolve result
问题
From the tutorial:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
const x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();
我可以看到10
一直传递到resolve(x)
,但似乎没有从resolve
函数中传递出去。文档说Promise.resolve
返回另一个Promise
,但我没有看到任何代码处理这个新的Promise
。我也没有看到任何代码将x
返回到外部范围。文档似乎没有解释这一点,或者我只是不理解。
await
关键字是如何知道返回值是10
的呢?
英文:
From the tutorial:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
const x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();
I can see 10
going all the way into the resolve(x)
, but it doesn't seem going out from the resolve
function. The document says Promise.resolve
returns another Promise
, but I don't see any code taking this new Promise
. I also don't see any code returning the x
to the outer scope. The document doesn't seem explaining this, or I just don't understand.
How does the await
keyword know that the return value is 10
?
答案1
得分: 1
因为 resolve
将 promise 的解决值设置为您传递给它的值,而 await
会等待直到 promise 被解决(或拒绝),然后返回解决的值(如果 promise 被拒绝则抛出异常)。
Promise.resolve
是一个不同的东西 - 它返回一个已经解决的 promise。
由于您将自己设计的新函数传递给 new Promise
,您可以将其命名为 resolve
,例如 ok
,如果这可以使事情更清晰:new Promise((ok) => ok(8))
(但请不要这样做,惯例是使用 resolve
)。
您实际上不需要知道 JavaScript 引擎内部发生了什么来使 promise 工作。
英文:
Because resolve
sets the promise's resolved value to what you pass to it, and await
awaits until a promise is resolved (or rejected) and returns the resolved value (or throws, if the promise was rejected).
Promise.resolve
is a different beast – it returns a promise that's already resolved.
Since you're passing a new function of your own devising to new Promise
, you could name that resolve
e.g. ok
, if it makes things clearer: new Promise((ok) => ok(8))
(but please don't; the convention is resolve
).
You don't really need to know what exactly happens internally in your JavaScript engine for promises to work.
答案2
得分: 1
以下是翻译好的部分:
- "The document says Promise.resolve"(文档中提到了Promise.resolve)
- "Do not confuse the static
resolve
method of thePromise
class with the function passed as the first argument to the function passed to thenew Promise
constructor."(不要将Promise
类的静态resolve
方法与传递给new Promise
构造函数的第一个参数的函数混淆。) - "I also don't see any code returning the x to the outer scope"(我也没有看到任何将
x
返回到外部作用域的代码) - "Because
x
isn't returned."(因为x
没有被返回。) - "You can't return from an asynchronous operation. That is why promises exist in the first place."(你无法从异步操作中返回。这就是为什么首次引入了Promise的原因。)
- "resolveAfter2Seconds returns a promise."(
resolveAfter2Seconds
返回一个Promise。) - "That promise is resolved with the value of
x
."(该Promise使用x
的值解析。) - "await sends
f1
to sleep until the promise on its RHS resolves."(await
使f1
进入休眠状态,直到右侧的Promise解析完成。) - "When it does, it becomes the resolved value which is assigned to the other
x
."(一旦解析完成,它就成为了解析值,被分配给另一个x
。)
英文:
> The document says Promise.resolve
Do not confuse the static resolve
method of the Promise
class with the function passed as the first argument to the function passed to the new Promise
constructor.
> I also don't see any code returning the x to the outer scope
Because x
isn't returned.
You can't return from an asynchronous operation. That is why promises exist in the first place.
resolveAfter2Seconds
returns a promise.
That promise is resolved with the value of x
.
await
sends f1
to sleep until the promise on its RHS resolves.
When it does, it becomes the resolved value which is assigned to the other x
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论