Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add for prefer-await-to-callbacks; fixes #118 #491

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ or start with the recommended rule set:
| [no-return-in-finally](docs/rules/no-return-in-finally.md) | Disallow return statements in `finally()`. | | ✅ | | |
| [no-return-wrap](docs/rules/no-return-wrap.md) | Disallow wrapping values in `Promise.resolve` or `Promise.reject` when not needed. | ✅ | | | |
| [param-names](docs/rules/param-names.md) | Enforce consistent param names and ordering when creating new promises. | ✅ | | | |
| [prefer-await-to-callbacks](docs/rules/prefer-await-to-callbacks.md) | Prefer async/await to the callback pattern. | | | | |
| [prefer-await-to-callbacks](docs/rules/prefer-await-to-callbacks.md) | Prefer `async`/`await` to the callback pattern. | | | | |
| [prefer-await-to-then](docs/rules/prefer-await-to-then.md) | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values. | | | | |
| [valid-params](docs/rules/valid-params.md) | Enforces the proper number of arguments are passed to Promise functions. | | ✅ | | |

Expand Down
36 changes: 35 additions & 1 deletion docs/rules/prefer-await-to-callbacks.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# Prefer async/await to the callback pattern (`promise/prefer-await-to-callbacks`)
# Prefer `async`/`await` to the callback pattern (`promise/prefer-await-to-callbacks`)

<!-- end auto-generated rule header -->

`async`/`await` is a clearer pattern to follow than using callbacks.

## Rule details

ES2017's `async`/`await` makes it easier to deal with asynchronous code than the
callback pattern.

Examples of **incorrect** code for this rule:

```js
cb()
callback()
doSomething(arg, (err) => {})
function doSomethingElse(cb) {}
```

Examples of **correct** code for this rule:

```js
await doSomething(arg)
async function doSomethingElse() {}
yield yieldValue(err => {})
eventEmitter.on('error', err => {})
```

## When Not To Use It

If you are not targeting an ES2017 or higher environment and cannot transpile
`async`/`await`, you should disable this rule.

## Further Reading

- [Making asynchronous programming easier with async and await on MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises)
2 changes: 1 addition & 1 deletion rules/prefer-await-to-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Prefer async/await to the callback pattern.',
description: 'Prefer `async`/`await` to the callback pattern.',
url: getDocsUrl('prefer-await-to-callbacks'),
},
messages: {
Expand Down