unicorn/no-thenable
Disallow a then property on objects, classes, and module exports
await and Promise.resolve() treat any object with a then method as a promise and call that method. If an object has then for another reason, awaiting it hangs or gives the wrong value. A module that exports then does the same to await import().
The rule reports then in an object literal, in a class, in an assignment such as response.then = ..., as a named export, and as the key passed to Object.defineProperty(), Reflect.defineProperty() or Object.fromEntries().
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-thenable" = "error" # or "warn"Examples#
example.js1 problem
const step = {
thenDo not add then to an object.(next) {
return next;
},
};
await step; // never finishes: `next` is not calledexample.js2 problems
class Query {
thenDo not add then to a class.() {}
}
export function thenDo not export then.() {}example.js2 problems
response.thenDo not add then to a class. = () => {};
Object.defineProperty(response, "then"Do not add then to an object., { value: 1 });example.jsNo problems
const step = {
andThen(next) {
return next;
},
};
fetch(url).then(response => response.json());When not to use it#
Turn it off where you implement a promise-like object on purpose, such as a lazy query builder that runs when it's awaited.