-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update errors reference, take 1 (#26391)
- Loading branch information
Showing
21 changed files
with
348 additions
and
488 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
files/en-us/web/javascript/reference/errors/bad_await/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
title: "SyntaxError: await is only valid in async functions, async generators and modules" | ||
slug: Web/JavaScript/Reference/Errors/Bad_await | ||
page-type: javascript-error | ||
--- | ||
|
||
{{jsSidebar("Errors")}} | ||
|
||
The JavaScript exception "await is only valid in async functions, async generators and modules" occurs when an {{jsxref("Operators/await", "await")}} expression is used outside of [async functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) or [modules](/en-US/docs/Web/JavaScript/Guide/Modules) or other async contexts. | ||
|
||
## Message | ||
|
||
``` | ||
SyntaxError: await is only valid in async functions and the top level bodies of modules (V8-based) | ||
SyntaxError: await is only valid in async functions, async generators and modules (Firefox) | ||
SyntaxError: Unexpected identifier (Safari) | ||
``` | ||
|
||
## Error type | ||
|
||
{{jsxref("SyntaxError")}}. | ||
|
||
## What went wrong? | ||
|
||
JavaScript execution is never blocking: an `await` can never block the execution of the program. Instead, it pauses the execution of the surrounding async task, while allowing other tasks to continue running. Therefore, `await` cannot be used in sync tasks, such as functions, generator functions, or top level of scripts. It is not always apparent whether the current file is a script or a module — see the [Modules guide](/en-US/docs/Web/JavaScript/Guide/Modules#top_level_await) for more information. | ||
|
||
## Examples | ||
|
||
### Top-level await | ||
|
||
You cannot use `await` at the top level of a script: | ||
|
||
```html example-bad | ||
<script> | ||
await fetch("https://example.com"); | ||
// SyntaxError: await is only valid in async functions, async generators and modules | ||
</script> | ||
``` | ||
|
||
Instead, make the script a module: | ||
|
||
```html example-good | ||
<script type="module"> | ||
await fetch("https://example.com"); | ||
</script> | ||
``` | ||
|
||
### Async callbacks | ||
|
||
You cannot use `await` in a sync callback: | ||
|
||
```js example-bad | ||
urls.forEach((url) => { | ||
await fetch(url); | ||
// SyntaxError: await is only valid in async functions, async generators and modules | ||
}); | ||
``` | ||
|
||
Instead, make the callback async. See more explanation in the [Using promises guide](/en-US/docs/Web/JavaScript/Guide/Using_promises#composition). | ||
|
||
```js example-good | ||
Promise.all(urls.map(async (url) => { | ||
await fetch(url); | ||
}); | ||
``` | ||
## See also | ||
- {{jsxref("Operators/await", "await")}} |
134 changes: 134 additions & 0 deletions
134
files/en-us/web/javascript/reference/errors/bad_break/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
--- | ||
title: "SyntaxError: unlabeled break must be inside loop or switch" | ||
slug: Web/JavaScript/Reference/Errors/Bad_break | ||
page-type: javascript-error | ||
--- | ||
|
||
{{jsSidebar("Errors")}} | ||
|
||
The JavaScript exception "unlabeled break must be inside loop or switch" occurs when a {{jsxref("Statements/break", "break")}} statement is not inside a loop or a {{jsxref("Statements/switch", "switch")}} statement. | ||
|
||
## Message | ||
|
||
``` | ||
SyntaxError: Illegal break statement (V8-based) | ||
SyntaxError: unlabeled break must be inside loop or switch (Firefox) | ||
SyntaxError: 'break' is only valid inside a switch or loop statement. (Safari) | ||
``` | ||
|
||
## Error type | ||
|
||
{{jsxref("SyntaxError")}}. | ||
|
||
## What went wrong? | ||
|
||
{{jsxref("Statements/break", "break")}} statements can be used to exit a loop or a `switch` statement, and using them elsewhere is a syntax error. Alternatively, you can provide a [label](/en-US/docs/Web/JavaScript/Reference/Statements/label) to the `break` statement to break out of any statement with that label — however, if the label does not reference a containing statement, another error [SyntaxError: label not found](/en-US/docs/Web/JavaScript/Reference/Errors/Label_not_found) will be thrown. | ||
|
||
## Examples | ||
|
||
### Unsyntactic break | ||
|
||
`break` cannot be used outside `switch` or loops. | ||
|
||
```js example-bad | ||
let score = 0; | ||
|
||
function increment() { | ||
if (score === 100) | ||
break; // SyntaxError: unlabeled break must be inside loop or switch | ||
} | ||
score++; | ||
} | ||
``` | ||
|
||
Maybe instead of `break`, you intend to use {{jsxref("Statements/return", "return")}} to early-terminate a function. | ||
|
||
```js example-good | ||
let score = 0; | ||
|
||
function increment() { | ||
if (score === 100) | ||
return; | ||
} | ||
score++; | ||
} | ||
``` | ||
|
||
### Using break in callbacks | ||
|
||
`break` cannot be used in callbacks, even if the callback is called from a loop. | ||
|
||
```js example-bad | ||
let containingIndex = 0; | ||
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | ||
|
||
while (containingIndex < matrix.length) { | ||
matrix[containingIndex].forEach((value) => { | ||
if (value === 5) { | ||
break; // SyntaxError: unlabeled break must be inside loop or switch | ||
} | ||
}); | ||
containingIndex++; | ||
} | ||
``` | ||
|
||
Instead, refactor the code so the `break` is used outside the callback. | ||
|
||
```js example-good | ||
let containingIndex = 0; | ||
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | ||
|
||
outer: while (containingIndex < matrix.length) { | ||
for (const value of matrix[containingIndex]) { | ||
if (value === 5) { | ||
break outer; | ||
} | ||
} | ||
containingIndex++; | ||
} | ||
``` | ||
|
||
```js example-good | ||
let containingIndex = 0; | ||
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | ||
|
||
while (containingIndex < matrix.length) { | ||
if (matrix[containingIndex].includes(5)) | ||
break; | ||
} | ||
containingIndex++; | ||
} | ||
``` | ||
|
||
There's no way to early-terminate a {{jsxref("Array/forEach", "forEach()")}} loop. You can use {{jsxref("Array/some", "some()")}} instead, or convert it to a {{jsxref("Statements/for...of", "for...of")}} loop. | ||
|
||
```js example-bad | ||
array.forEach((value) => { | ||
if (value === 5) { | ||
break; // SyntaxError: unlabeled break must be inside loop or switch | ||
} | ||
// do something with value | ||
}); | ||
``` | ||
|
||
```js example-good | ||
array.some((value) => { | ||
if (value === 5) { | ||
return true; | ||
} | ||
// do something with value | ||
}); | ||
``` | ||
|
||
```js example-good | ||
for (const value of array) { | ||
if (value === 5) { | ||
break; | ||
} | ||
// do something with value | ||
} | ||
``` | ||
|
||
## See also | ||
|
||
- {{jsxref("Statements/break", "break")}} |
Oops, something went wrong.