大网数据广告活动
Web 标准与前端开发

SyntaxError: await/yield expression can't be used in parameter

· 52CE 我爱测

The JavaScript exception "await expression can't be used in parameter" or "yield expression can't be used in parameter" occurs when the default parameter expression contains the Operators/await or Operators/yield keyword and has the effect of pausing default parameter evaluation.

The JavaScript exception "await expression can't be used in parameter" or "yield expression can't be used in parameter" occurs when the default parameter expression contains the Operators/await or Operators/yield keyword and has the effect of pausing default parameter evaluation.

Message

SyntaxError: Illegal await-expression in formal parameters of async function (V8-based)
SyntaxError: await expression can't be used in parameter (Firefox)
SyntaxError: Cannot use 'await' within a parameter default expression. (Safari)

SyntaxError: Yield expression not allowed in formal parameter (V8-based)
SyntaxError: yield expression can't be used in parameter (Firefox)
SyntaxError: Unexpected keyword 'yield'. Cannot use yield expression within parameters. (Safari)

Error type

SyntaxError

What went wrong?

The default expression must be able to evaluate synchronously. If it contains an await or yield expression, it will pause the evaluation of the default expression, which is not allowed.

[!NOTE] This error is only generated when await or yield are valid operators in this function context. Otherwise, await or yield would be parsed as an identifier, and either not cause an error, or cause an error like "reserved identifier", or "unexpected token" if there's an expression following it.

Examples

Invalid cases

function *gen(a = yield 1) {}

async function f(a = await Promise.resolve(1)) {}

Valid cases

You can use the nullish coalescing assignment to provide a default value instead. If you want to treat null and undefined differently, you would need to use a condition.

function* gen(a) {
  a ??= yield 1;
}

async function f(a) {
  a ??= await Promise.resolve(1);
}

You are also allowed to use await or yield if the expression is contained in a function expression of the initializer and would not pause the evaluation of the default expression.

async function f(a = (async () => await Promise.resolve(1))()) {}

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。