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 = {
  then(next) {
    return next;
  },
};

await step; // never finishes: `next` is not called
example.js2 problems
class Query {
  then() {}
}

export function then() {}
example.js2 problems
response.then = () => {};

Object.defineProperty(response, "then", { 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.