How can I determine if a jQuery object is deferred?

后端 未结 3 1800
醉梦人生
醉梦人生 2020-12-11 00:21

If I have a function that sometimes returns a deferred object but sometimes a non-deferred object. How can I tell which one it is?

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 00:57

    Inspired by Niko's answer, I created another implementation that would check if an object is a deferred based on the name of it's properties but also on the content of those properties. I had to do so since an other object of mine had a property named promise.

    if (typeof value.resolve !== "function") {
      return false;
    }
    return String(value.resolve) === String($.Deferred().resolve);
    

提交回复
热议问题