How can I tell if an object is a jQuery Promise/Deferred?

后端 未结 2 396
生来不讨喜
生来不讨喜 2021-02-05 03:05

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 val

2条回答
  •  执笔经年
    2021-02-05 03:37

    The quick-and-dirty solution is to test if the object has a then function:

    if (typeof message.then === 'function') {
        //assume it's a Deferred or fits the Deferred interface
    } else {
        //do other stuff
    }
    

提交回复
热议问题