在量角器.js 中,
我有承诺/延迟的功能。例如
var myFunc = function(_params) {
var deferred = protractor.promise.defer();
/***do magical code things****/
/***wait for other promises***/
/*****deferred.fulfill();*****/
return deferred.promise;
};
什么样的组合typeof
我可以使用语句来检查这个东西(当传递给其他东西时)是否承诺?
typeof promiseMaybe === 'function'
-
typeof promiseMaybe.then === 'function'
或者有没有一个非typeof
功能就像...
promiseMaybe.isThenable
protractor.promise.isThenable(promiseMaybe)
澄清
我有一个方法可以接收myFunc
作为参数,但此方法还可以接收字符串和查找器。我需要知道如何判断参数是否是承诺某些内容的函数,可能是在调用函数之前。
Protractor 中有一个辅助方法 -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 直接从selenium-webdriver
, 在这里你可以找到该方法的源代码:
/**
* 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';
};
所以基本上,任何带有then
方法被认为是一个 Promise。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)