Fix TypeError then Is Not a Function in JavaScript
Fix TypeError then is not a function by finding the value that is not a promise, from missing returns to callback APIs and mocks, with debugging steps.

The error TypeError: x.then is not a function appears when code calls .then() on a value that is not a promise. The name before .then varies with your code, for example TypeError: getUser(...).then is not a function or TypeError: result.then is not a function. The cause is always the same, a function that you expected to return a promise returned something else, such as undefined, a plain object, an array or a callback-based result. This post shows how to read the message, five common causes with failing code and fixes, a quick way to inspect the value and how to guard against it. The examples run on Node.js 24.
What the error says
JavaScript is telling you that the thing on the left of .then does not have a method called then. It does not say what the thing is. The first debugging step is to print it.
function getUser() {
return { id: 1, name: "Ada" }; // returns an object, not a promise
}
try {
getUser().then((user) => console.log(user.name));
} catch (error) {
console.log(error.name + ": " + error.message);
console.log("typeof:", typeof getUser(), "has then:", typeof getUser().then);
}TypeError: getUser(...).then is not a function
typeof: object has then: undefinedThe message shows the call expression, getUser(...), because V8 renders the callee for you. The second line confirms the value is a plain object with no then. In your code, that print is the fastest way to learn what the value really is.
Cause 1: a function that is not async and returns a plain value
The function was meant to be asynchronous and was not. Either wrap the value in a promise, or mark the function async, which makes it always return one.
async function getUser() {
return { id: 1, name: "Ada" };
}
getUser().then((user) => console.log(user.name));AdaIf the function does real asynchronous work, async is the right tool. If you cannot change it, call Promise.resolve(getUser()), which turns any value into a promise and passes a promise through unchanged, so it is a safe way to handle values that might be either.
Cause 2: a missing return
A function that creates a promise inside but forgets return gives undefined to the caller.
function loadData() {
Promise.resolve([1, 2, 3]); // result thrown away
}
try {
loadData().then((rows) => console.log(rows));
} catch (error) {
console.log(error.message);
}Cannot read properties of undefined (reading 'then')This variant reports a different message, because the value is undefined and the property read fails first. Searching the web for Cannot read properties of undefined (reading 'then') leads here. The fix is return Promise.resolve([1, 2, 3]);. Arrow functions with a block body have the same trap: () => { fetchData(); } returns nothing, while () => fetchData() returns the promise.
Cause 3: a callback-based API
Many Node.js functions take a callback and return nothing, or return a different object, and have no .then. Calling .then on the result of fs.readFile from node:fs throws, because that is the callback version.
import fs from "node:fs";
import fsp from "node:fs/promises";
try {
fs.readFile(import.meta.filename, "utf8").then(() => {});
} catch (error) {
console.log(error.name);
}
const text = await fsp.readFile(import.meta.filename, "utf8");
console.log(text.length > 0);TypeError
trueThe first call throws before reading anything, since fs.readFile requires a callback argument and throws ERR_INVALID_ARG_TYPE when it is missing. The node:fs/promises module has the promise versions. For a library with only a callback API, wrap it with util.promisify, as shown in turning a callback API into a promise.
Cause 4: a mock or stub that returns the wrong thing
In tests, a mocked function often returns a plain value while the real one returns a promise. The code under test calls .then and the test fails with this error, although production works.
import { mock } from "node:test";
const realShape = mock.fn(() => ({ ok: true })); // wrong: real function returns a promise
const fixed = mock.fn(async () => ({ ok: true }));
console.log(typeof realShape().then, typeof fixed().then);undefined functionMake mocks return promises when the real function does. With node:test mocks use mock.fn(async () => value). In Jest, mockResolvedValue(value) is the equivalent. Type-checked tests catch this at compile time.
Cause 5: a library returns different things in different modes
Some libraries return a promise when you call them without a callback, and undefined when you pass one. Others return a promise only if you opt in, or return a query builder object that is thenable only when you call a final method such as exec(). If .then is missing on an object you got from such a library, read its documentation for the method that executes the query. Mongoose queries, for example, are thenable, while a half-built query builder in other libraries is not.
Cause 6: awaiting the wrong layer
Sometimes the code is await getUser().then(...) where getUser is already awaited somewhere above, or the value is an array of promises, not a promise.
const jobs = [Promise.resolve(1), Promise.resolve(2)];
try {
jobs.then((values) => console.log(values));
} catch (error) {
console.log(error.message);
}
console.log(await Promise.all(jobs));jobs.then is not a function
[ 1, 2 ]An array has no .then. Wrap it in Promise.all to get a promise of an array of results.
A guard when the value might be either
If an API legitimately returns either a value or a promise, normalize it instead of checking the type everywhere.
function maybeAsync(flag) {
return flag ? Promise.resolve("async value") : "sync value";
}
for (const flag of [true, false]) {
console.log(await Promise.resolve(maybeAsync(flag)));
}async value
sync valueawait itself also accepts non-promises and returns them unchanged, so await maybeAsync(false) works too. The .then method, in contrast, requires the value to be thenable first. That asymmetry is why await is more forgiving than .then and is often the simplest fix: change x.then(fn) into fn(await x) inside an async function.
Debugging checklist
When the error appears, print typeof x and x right before the failing line. If it is undefined, look for a missing return or a function that was never meant to return a value. If it is an object with other methods, look for a callback or builder API. If it is an array, use Promise.all. If the failure only occurs in tests, compare the mock to the real signature. TypeScript prevents most of these: with strict types, calling .then on a User is a compile error, and @typescript-eslint/await-thenable flags await on non-promises.
Where the advice stops
Making every function async to avoid this error is not a fix. It hides the question of which functions are really asynchronous and adds a microtask turn to calls that did not need it. Mark functions async when they await, or when they are part of an interface that must return a promise. For everything else, keep the return type honest.
Reading the stack trace
The stack trace for this error points at the line that called .then, and the line is nearly always correct. What it cannot tell you is where the wrong value came from, so follow the value back. Start at the failing call, find the function that produced the value, and read its return statements. A function with several return paths is a common source: one branch returns a promise, another returns a cached plain value. That inconsistency is a design bug in its own right, because callers cannot know which they will get. The repair is to make every path return the same kind of thing, usually by marking the function async, so even the cached branch returns a promise.
Caching code is the classic example. A function checks a map and returns the stored value on a hit, and returns a fetch promise on a miss. The first call works, since it misses and gets a promise. The second call hits the cache and returns a plain object, and the caller's .then throws. Caching the promise instead of the value, as in cache.set(key, promise), keeps the return type the same on every call and also makes concurrent requests share one fetch.
Preventing it in TypeScript projects
In TypeScript the compiler catches nearly all of these before the code runs, provided the types are honest. Declare return types on exported functions, such as Promise<User>, instead of leaving them inferred, so a change inside the function that breaks the contract becomes a compile error at the function, not a runtime error far away. Turn on strict and the lint rules @typescript-eslint/no-floating-promises and @typescript-eslint/await-thenable. The second reports await on a value that is not a promise, which is usually a sign that the author misjudged what a function returns. With those in place, the runtime error becomes rare and, when it does occur, almost always comes from an any that hid the mismatch.


