“TypeError: then is not a function” In JavaScript – How To Solve It?

“TypeError: then is not a function” in JavaScript

Today we will solve an annoying error when working with Promises, it is “TypeError: then is not a function” in JavaScript. Before diving into the cause of this error, let’s learn a little about Promise! Let’s start!

What is Promise?

The Promise is a mechanism in JavaScript that helps you execute asynchronous tasks without falling into callback hell. Asynchronous tasks that can send AJAX requests and call functions inside setTimeout, setInterval, etc.

A promise has three states: Pending, Fulfilled and Rejected.

We use the then() function for the fulfilled state and the catch() function for the rejected state.

That’s it for the Promise. Now let’s find out why we get the error Keyword: “TypeError: then is not a function” in JavaScript.

When does the error “TypeError: then is not a function” in JavaScript occur?

The then() function is an attribute of the promise object. This error will occur if you try to use then() on a non-promise object. See the example below for a better understanding:

let promise = new Promise((resolve, reject) => {
  resolve('This is learnshareIT');
});

let str = 'This is a string';

promise.then((value) => {
  console.log(value); // Success!
});

str.then((value) => {
  console.log(value); // Error because str is not a Promise
});

Output:

This is learnshareIT
Error: str.then is not a function

If you have read this far, you must have a solution for this error, right? That’s right, just call the then() function on a Promise object.

How to know if an object is a promise or not?

Using toString() method

Syntax:

toString()

Description:

By default, toString() takes no parameters, and this function returns a string representing the object calling it.

toString() function will turn a Promise object into a string “[object Promise]”. Therefore, we can check if a variable is a Promise or not. Like this:

let promise = new Promise((resolve, reject) => {
  resolve('This is learnshareIT');
});

let str = 'This is a string';

function isPromise(value) {
  if (value.toString() === '[object Promise]') {
    return true;
  }
  return false;
}

console.log(isPromise(promise));
console.log(isPromise(str));

Output:

true
false

From now on, use the isPromise() function to check any variable before using then(). We won’t get the “TypeError: then is not a function” again.

Using instanceof operator

Syntax:

object instanceof constructor

Parameters:

  • object: an object you want to test.
  • constructor: a constructor function you want to test such as array, object, string …

With the instanceof, we only need a few lines of code to be able to check if a variable is a Promise or not. Like this:

let promise = new Promise((resolve, reject) => {
  resolve('This is learnshareIT');
});

let str = 'This is a string';

function isPromise(value) {
  return value instanceof Promise;
}

console.log(isPromise(promise));
console.log(isPromise(str));

Output:

true
false

Using constructor property

Syntax:

object.constructor

Description:

In JavaScript, the object is created from a constructor, and an object’s constructor property will return exactly the constructor function that created the object.

Based on the description, we can quickly check if an object is a Promise or not. Like this:

let promise = new Promise((resolve, reject) => {
  resolve('This is learnshareIT');
});

let str = 'This is a string';

function isPromise(value) {
  return value.constructor === Promise;
}

console.log(isPromise(promise));
console.log(isPromise(str));

Output:

true
false

Summary

In general, the error “TypeError: then is not a function” in JavaScript is an easy one to fix. We can avoid this error if we don’t use then() on non-Promise objects. Hopefully, this is useful for you.

Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *