简体   繁体   English

为什么可以在等待后在控制台中重新分配 `const x`?

[英]Why is it possible to reassign `const x` in the console after an await?

EDIT: I discovered you don't even need the function.编辑:我发现你甚至不需要这个功能。 This works too for me:这对我也有效:

const x = await 'hello'
x = 'something else'

Note that without the await it throws an error.请注意,没有await它会引发错误。

By first defining an async function,首先定义一个异步函数,

async function a() {
  return new Promise(resolve => {
    resolve('hello')
  })
}

Then assigning x to 'hello' ,然后将x分配给'hello'

const x = await a()

Then reassigning x .然后重新分配x

x = 'something else'

If x is a constant variable, why can it be reassigned?如果x是一个常量变量,为什么可以重新赋值? Is there something obvious I'm missing here?有什么明显的东西我在这里失踪了吗?

Here's the complete code (tested in browser console on Chrome 80.0.3987.87):这是完整的代码(在 Chrome 80.0.3987.87 上的浏览​​器控制台中测试):

async function a() {
  return new Promise(resolve => {
    resolve('hello')
  })
}

const x = await a()

x = 'something else'

I am unable to reproduce what you describe.我无法重现您所描述的内容。 It throws an error for me.它给我抛出了一个错误。

 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()); } })()

With your updated question, i see that this occurs when you enter the code in the chrome developer console.通过您更新的问题,我看到当您在 chrome 开发人员控制台中输入代码时会发生这种情况。 Futhermore, your code is using await outside an async function.此外,您的代码在 async 函数之外使用 await 。 Top-level await is not yet a part of the javascript standard, though it is working its way through the approval process .顶级 await 还不是 javascript 标准的一部分,尽管它正在通过审批流程 The dev tools do let you do it, but i would not be surprised if the behavior that results is nonstandard.开发工具确实可以让您这样做,但如果结果的行为是非标准的,我不会感到惊讶。

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..更新:我可以在 Firefox 72.0.2(64 位)中重现错误,但是如果你将它包装在一个async函数中,它的行为就不会那样了..

To know more about: ECMAScript proposal: Top-level await要了解更多信息: ECMAScript 提案:顶级等待

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 '';您可以等待字符串,因为它是允许的: block-await-expr-literal-string.js , AwaitExpression StringLiteral (在块中顶级等待的有效语法。)因此const x = await ''; is valid syntax是有效的语法

(async () => {
    async function a() {
        return new Promise(resolve => {
            resolve('hello')
        })
    }

    const x = await a()

    x = 'something else'

})();

Hmm, probably you're missing the console debugger..嗯,可能你错过了控制台调试器..

async function a() {
  return new Promise(resolve => {
    resolve('hello')
  })
}

const x = await a()

x = 'something else'
x = 'not assignable... in console or whatever..';

类型错误

执行暂停..

You shouldn't be using await keyword in sync function (your func here is the global scope).你不应该在同步函数中使用await关键字(你的 func 是全局范围)。 It doesn't make sense.这没有意义。 We are using await to wait for some async operation for ex.我们正在使用await等待一些异步操作。 API call. API 调用。

If you remove await it will normally throw an error.如果你删除await它通常会抛出一个错误。

First of all please don't use browser console as an ideal editor environment when you are learning these things.首先,在学习这些东西时,请不要将浏览器控制台用作理想的编辑器环境。

Secondly I just checked what you mentioned in the browser console.其次,我刚刚检查了您在浏览器控制台中提到的内容。

If you simply copy and past your whole code once it gives an error as expected saying constants can't be reassigned.如果您只是简单地复制并传递整个代码,它会按预期给出错误,说明无法重新分配常量。 as clearly mentioned in the @some user's answer.正如@some 用户的回答中明确提到的那样。

But if you copy and past it line by line it won't give you any errors.但是,如果您逐行复制并粘贴它,它不会给您任何错误。

I'm not exactly sure what is the reason but you can't expect browser console to provide you with 100% compiler/validation environment.我不确定是什么原因,但您不能指望浏览器控制台为您提供 100% 编译器/验证环境。

So unless it is some simple expression evaluation it is best to use a good development environment so you won't get confused by unexpected behaviors.所以除非是一些简单的表达式求值,否则最好使用一个好的开发环境,这样你就不会被意外的行为弄糊涂了。

If you still need something that runs on the browser try this http://www.typescriptlang.org/play/如果你仍然需要在浏览器上运行的东西试试这个http://www.typescriptlang.org/play/

EDIT: I found this comment on @Nicholas Tower's answer and it seems like the correct answer to this question编辑:我在@Nicholas Tower 的回答上发现了这条评论,这似乎是这个问题的正确答案

"The top-level-await proposal is for modules only, it won't have any effect on await in the devtools console. – Bergi" “顶级await 提议仅适用于模块,它不会对devtools 控制台中的await 产生任何影响。– Bergi”

Happy coding!!!快乐编码!!!

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

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