Skip to content

Commit

Permalink
docs: add for prefer-await-to-callbacks; fixes #118 (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 authored Jul 21, 2024
1 parent c011a1a commit 36d13f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
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

0 comments on commit 36d13f5

Please sign in to comment.