为什么可以在等待后在控制台中重新分配 `const x`?
Why is it possible to reassign `const x` in the console after an await?
编辑: 我发现你甚至不需要这个功能。这对我也有效:
const x = await 'hello'
x = 'something else'
请注意,如果没有 await
,它会引发错误。
首先定义一个异步函数,
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
然后将x
赋值给'hello'
,
const x = await a()
然后重新分配 x
。
x = 'something else'
如果x
是常量变量,为什么可以重新赋值呢?我在这里明显遗漏了什么吗?
这是完整的代码(在 Chrome 80.0.3987.87 上的浏览器控制台中测试):
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
const x = await a()
x = 'something else'
我无法重现您描述的内容。它为我抛出一个错误。
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
(async () => {
try {
const x = await a();
x = 'something else';
console.log("worked", x);
} catch (err) {
console.log("didn't work", err.toString());
}
})()
关于你更新的问题,我看到当你在 chrome 开发者控制台中输入代码时会发生这种情况。此外,您的代码在异步函数之外使用 await 。顶级 await 还不是 javascript 标准的一部分,尽管它正在按照自己的方式工作 through the approval process。开发工具确实允许您这样做,但如果结果行为不标准,我不会感到惊讶。
Update: I'm able to reproduce the error in: Firefox 72.0.2 (64-bit),
However if you wrap it within an async
function it doesn't behave like that..
了解更多:ECMAScript proposal: Top-level await
You can await strings because it's permitted: block-await-expr-literal-string.js,
AwaitExpression StringLiteral (Valid syntax for top level await in a
block.)
Thus const x = await '';
is valid syntax
(async () => {
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
const x = await a()
x = 'something else'
})();
嗯,您可能缺少控制台调试器..
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
const x = await a()
x = 'something else'
x = 'not assignable... in console or whatever..';
你不应该在 sync 函数中使用 await 关键字(你的 func 是全局范围)。
这没有意义。我们正在使用 await 来等待 ex 的一些异步操作。 API 打电话。
如果删除 await 它通常会抛出错误。
首先请不要在学习这些东西的时候使用浏览器控制台作为理想的编辑器环境。
其次,我刚刚检查了您在浏览器控制台中提到的内容。
如果您只是简单地复制并粘贴您的整个代码,一旦它如预期的那样给出一个错误,说常量无法重新分配。正如@some 用户的回答中明确提到的那样。
但是,如果您逐行复制并粘贴它,则不会出现任何错误。
我不太确定是什么原因,但您不能指望浏览器控制台为您提供 100% compiler/validation 环境。
所以除非是一些简单的表达式计算,否则最好使用良好的开发环境,这样你就不会被意外行为搞糊涂了。
如果您仍然需要在浏览器上运行的东西,试试这个
http://www.typescriptlang.org/play/
编辑: 我在@Nicholas Tower 的回答中找到了这条评论,它似乎是这个问题的正确答案
"The top-level-await proposal is for modules only, it won't have any
effect on await in the devtools console. – Bergi"
编码愉快!!!
编辑: 我发现你甚至不需要这个功能。这对我也有效:
const x = await 'hello'
x = 'something else'
请注意,如果没有 await
,它会引发错误。
首先定义一个异步函数,
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
然后将x
赋值给'hello'
,
const x = await a()
然后重新分配 x
。
x = 'something else'
如果x
是常量变量,为什么可以重新赋值呢?我在这里明显遗漏了什么吗?
这是完整的代码(在 Chrome 80.0.3987.87 上的浏览器控制台中测试):
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
const x = await a()
x = 'something else'
我无法重现您描述的内容。它为我抛出一个错误。
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
(async () => {
try {
const x = await a();
x = 'something else';
console.log("worked", x);
} catch (err) {
console.log("didn't work", err.toString());
}
})()
关于你更新的问题,我看到当你在 chrome 开发者控制台中输入代码时会发生这种情况。此外,您的代码在异步函数之外使用 await 。顶级 await 还不是 javascript 标准的一部分,尽管它正在按照自己的方式工作 through the approval process。开发工具确实允许您这样做,但如果结果行为不标准,我不会感到惊讶。
Update: I'm able to reproduce the error in: Firefox 72.0.2 (64-bit), However if you wrap it within an
async
function it doesn't behave like that..
了解更多:ECMAScript proposal: Top-level await
You can await strings because it's permitted: block-await-expr-literal-string.js, AwaitExpression StringLiteral (Valid syntax for top level await in a block.) Thus
const x = await '';
is valid syntax
(async () => {
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
const x = await a()
x = 'something else'
})();
嗯,您可能缺少控制台调试器..
async function a() {
return new Promise(resolve => {
resolve('hello')
})
}
const x = await a()
x = 'something else'
x = 'not assignable... in console or whatever..';
你不应该在 sync 函数中使用 await 关键字(你的 func 是全局范围)。 这没有意义。我们正在使用 await 来等待 ex 的一些异步操作。 API 打电话。
如果删除 await 它通常会抛出错误。
首先请不要在学习这些东西的时候使用浏览器控制台作为理想的编辑器环境。
其次,我刚刚检查了您在浏览器控制台中提到的内容。
如果您只是简单地复制并粘贴您的整个代码,一旦它如预期的那样给出一个错误,说常量无法重新分配。正如@some 用户的回答中明确提到的那样。
但是,如果您逐行复制并粘贴它,则不会出现任何错误。
我不太确定是什么原因,但您不能指望浏览器控制台为您提供 100% compiler/validation 环境。
所以除非是一些简单的表达式计算,否则最好使用良好的开发环境,这样你就不会被意外行为搞糊涂了。
如果您仍然需要在浏览器上运行的东西,试试这个 http://www.typescriptlang.org/play/
编辑: 我在@Nicholas Tower 的回答中找到了这条评论,它似乎是这个问题的正确答案
"The top-level-await proposal is for modules only, it won't have any effect on await in the devtools console. – Bergi"
编码愉快!!!