# promise/prefer-await-to-callbacks

> Require `async`/`await` instead of error-first callbacks

With callbacks, every step has to check `err` by hand and pass it on. A forgotten check drops the error, and `try`/`catch` around the call doesn't catch anything that happens later. With `await`, errors are thrown and the steps read from top to bottom.

The rule goes by names. It reports:

- a call to a function named `callback` or `cb`
- a function with a last parameter named `callback` or `cb`
- a function passed as the last argument, if its first parameter is named `err` or `error`

The last case has exceptions: event listeners (`on`, `once`, `addEventListener`, `removeEventListener`), the array methods `map`, `every`, `forEach`, `some`, `find` and `filter`, and calls inside an `await` or `yield` expression.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"promise/prefer-await-to-callbacks" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 4} */}
```js
function readConfig(path, callback) {
  fs.readFile(path, "utf8", (err, text) => {
    if (err) return callback(err);
    callback(null, JSON.parse(text));
  });
}
```

Not reported:

{/* good */}
```js
import { readFile } from "node:fs/promises";

async function readConfig(path) {
  const text = await readFile(path, "utf8");
  return JSON.parse(text);
}

server.on("error", err => console.error(err));
failures.forEach(err => console.error(err));
await retry(upload, err => err.code === "ETIMEDOUT");
```

## When not to use it

Leave it off in code that has to offer or use a callback API, such as Express middleware or older Node.js libraries.
