Skip to content

Commit

Permalink
async
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Nov 5, 2024
1 parent d4336eb commit c885683
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions .grit/patterns/js/es6_arrow_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The following pattern transforms JS traditional functions to arrow functions.
To see how it works, follow the tutorial.
*/
or {
// Rewrite traditional functions to arrow functions
or {
`async function ($args) { $body }` => `async ($args) => {
$body
Expand All @@ -27,13 +28,17 @@ or {
or { `this`, `arguments` }
} until `function $_($_) { $_ }`
},
`($args) => { return $value }` where {
if ($value <: object()) {
$result = `($value)`
// Rewrite arrow functions to remove unnecessary return statements
or {
`async ($args) => { return $value }` where $async = `async `,
`($args) => { return $value }` where $async = .,
} where {
if ($value <: object()) {
$result = `($value)`
} else {
$result = $value
}
} => `($args) => $result`
} => `$async($args) => $result`
}
```

Expand Down Expand Up @@ -124,3 +129,19 @@ const a = {
},
};
```

## Handles async return values

When removing an unnecessary `return` statement, we still need to consider if the function is async.

```js
const a = async () => {
return await Promise.resolve(1);
};
```

After:

```js
const a = async () => await Promise.resolve(1);
```

0 comments on commit c885683

Please sign in to comment.