Checking if an object is a promising function

后端 未结 1 647
遇见更好的自我
遇见更好的自我 2021-01-15 14:23

In protractor.js,

I have functions that promise/defer. For example

var myFunc = function(_params) {
  var deferred = protractor.promise.defer();
           


        
1条回答
  •  星月不相逢
    2021-01-15 14:59

    There is a helper method in Protractor for that - protractor.promise.isPromise():

    var el = element(by.css('foo'));
    
    protractor.promise.isPromise('foo'); // false
    protractor.promise.isPromise(el); // false
    protractor.promise.isPromise(el.click()); // true
    

    Protractor takes this method directly from selenium-webdriver, here you can find the source code of the method:

    /**
     * Determines whether a {@code value} should be treated as a promise.
     * Any object whose "then" property is a function will be considered a promise.
     *
     * @param {*} value The value to test.
     * @return {boolean} Whether the value is a promise.
     */
    promise.isPromise = function(value) {
      return !!value && goog.isObject(value) &&
          // Use array notation so the Closure compiler does not obfuscate away our
          // contract. Use typeof rather than goog.isFunction because
          // goog.isFunction accepts instanceof Function, which the promise spec
          // does not.
          typeof value['then'] === 'function';
    };
    

    So basically, any object with a then method is considered a Promise.

    0 讨论(0)
提交回复
热议问题