promise/no-promise-in-callback
Disallow promises inside error-first callbacks
The code that calls a callback ignores what it returns. Nobody awaits a promise started inside one, so a rejection goes unhandled, and the code now reports errors in two different ways.
A function counts as a callback if its first parameter is named err or error. Inside one, the rule reports calls to then(), catch(), finally() and static methods such as Promise.all().
A call that's the value of a return statement isn't reported. A function passed to then() or catch() doesn't count as a callback, no matter what its first parameter is called.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"promise/no-promise-in-callback" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"promise/no-promise-in-callback" = ["error", {
exemptDeclarations = false,
}]Examples#
example.js1 problem
fs.readFile(path, "utf8", (err, text) => {
if (err) throw err;
upload(text).thenAvoid using promises inside of callbacks.(notify);
});example.jsNo problems
import { readFile } from "node:fs/promises";
async function publish(path) {
const text = await readFile(path, "utf8");
await upload(text);
notify();
}
upload(draft).catch(err => retry(draft).then(notify));Options#
| Option | Default | What it does |
|---|---|---|
exemptDeclarations | false | Don't treat function declarations as callbacks, only function expressions and arrow functions. |
example.js1 problem
function onFailure(error) {
report(error).thenAvoid using promises inside of callbacks.(close);
}example.jswith
{ "exemptDeclarations": true }No problemsfunction onFailure(error) {
report(error).then(close);
}