Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:51:14+00:00 2026-06-14T11:51:14+00:00

I have a function that takes a single argument. I need to be able

  • 0

I have a function that takes a single argument. I need to be able to tell if this argument is a jQuery Promise or Deferred object. If not, then the value may be of any type and have any properties, so it’s not safe to just just for the presence of the promise methods.

Here’s an example of how I’d like my function to behave:

function displayMessage(message) {
  if (message is a Promise or Deferred) {
    message.then(displayMessage);
  } else {
    alert(message);
  }
}

Notice the recursive handling of promises: if a promise is resolved with another promise value we don’t display it, we wait for it to be resolved. If it returns yet another promise, repeat.


This is important because if this were not the case, I would just be able to use jQuery.when:

function displayMessage(message) {
  jQuery.when(message).then(function(messageString) {
    alert(messageString);
  });
}

This would handle values and promises of values correctly…

displayMessage("hello");                            // alerts "hello"
displayMessage(jQuery.Deferred().resolve("hello")); // alerts "hello"

…but once we get to promises of promises of values, it breaks down:

displayMessage(jQuery.Deferred().resolve(
  jQuery.Deferred().resolve("hello")
));                                                 // alerts "[object Object]"

jQuery.when is able to tell if a value is promise, so apparently it is possible. How can I check?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:51:16+00:00Added an answer on June 14, 2026 at 11:51 am

    jQuery.when is able to tell if a value is promise, so apparently it is possible.

    This is mistaken. jQuery itself is not able to check if an object is a promise with complete accuracy. If you look at the source of jQuery.when in the jQuery source viewer you can see that all it does is this:

    jQuery.isFunction(firstParam.promise)
    

    If the object you are returning has its own .promise() method, jQuery.when will misbehave:

    var trickyValue = {
      promise: function() { return 3; },
      value: 2
    };
    
    jQuery.when(trickyValue).then(function(obj) {
      alert(obj.value);
    });
    

    This throws TypeError: Object 3 has no method 'then', because jQuery assumes the object is a promise and trusts the value of its .promise() method.

    This is probably impossible to solve properly. The promise object is created as an object literal inside of jQuery.Deferred (view source). It has no prototype, nor any other truly unique properties that could be used to distinguish it.

    However, I can think of a hacky solution that should be reliable as long as only one version of jQuery is in use:

    function isPromise(value) {
      if (typeof value === 'object' && typeof value.then !== "function") {
        return false;
      }
      var promiseThenSrc = String($.Deferred().then);
      var valueThenSrc = String(value.then);
      return promiseThenSrc === valueThenSrc;
    }
    
    isPromise("test");                 // false
    isPromise($.Deferred());           // true
    isPromise($.Deferred().promise()); // true
    

    Converting a function to a string gives you its source code, so here I am comparing then source of the .then method of a new Deferred object to that of the value I’m interested in. Your value is not going to have a .then method with exactly the same source code as a jQuery.Deferred or Promise1.

    1. Unless you’re running in a hostile environment, in which case you should probably give up.


    If you aren’t specifically interested in jQuery promises, but would like to detect any type of Promise including the built-in ones from ECMAScript 6, you can test if value is an object and has a then method:

    if (typeof value === 'object' && typeof value.then === 'function') {
      // handle a promise
    } else {
      // handle a concrete value
    }
    

    This is the approach by several Promise-handling functions defined in ES6. You can see this described in the specification of the resolve(...) functions, partially quoted below:

    When a promise resolve function F is called with argument resolution, the following steps are taken:

    […]

    1. If Type(resolution) is not Object, then
      1. Return FulfillPromise(promise, resolution).
    2. Let then be Get(resolution, "then").
    3. If then is an abrupt completion, then
      1. Return RejectPromise(promise, then.[[value]]).
    4. Let thenAction be then.[[value]].
    5. If IsCallable(thenAction) is false, then
      1. Return FulfillPromise(promise, resolution).
    6. Perform EnqueueJob ("PromiseJobs", PromiseResolveThenableJob, «‍promise, resolution, thenAction»)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that takes a string-like argument. I want to decide if
I have a function that takes an array of objects and I need to
I have this function that takes the input of a, runs a calculation and
I have a function that take an argument which can be either a single
So I have this function that takes all the users from an MySql table
I have a simple javascript function that takes two variables. I need to pass
I have a function that takes an input string and then runs the string
I have a function that takes a By element, By by = By.xpath(xpathString). Inside
I have a function that takes a const D3DVECTOR3 *pos , but I have
I have a function that takes 2 parameters : 1 = XML file, 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.