q
library offers a unique feature that allows resolving and spreading multiple promises into separate arguments:
If you have a promise for an array, you can utilize spread instead of then. The spread function distributes the values as arguments in the fulfillment handler.
return getUsername()
.then(function (username) {
return [username, getUser(username)];
})
.spread(function (username, user) {
});
In our work with protractor, we are exploring the capabilities of the built-in protractor.promise
from WebDriverJS
.
The Query:
Is it feasible to implement the "spread" functionality using protractor.promise
?
A Practical Example:
We have developed a custom jasmine matcher to verify if an element is focused. This requires resolving two promises before conducting an equality comparison. Currently, we rely on protractor.promise.all()
and then()
:
protractor.promise.all([
elm.getId(),
browser.driver.switchTo().activeElement().getId()
]).then(function (values) {
jasmine.matchersUtil.equals(values[0], values[1]);
});
Ideally, we would prefer a more readable approach such as:
protractor.promise.all([
elm.getId(),
browser.driver.switchTo().activeElement().getId()
]).spread(function (currentElementID, activeElementID) {
return jasmine.matchersUtil.equals(currentElementID, activeElementID);
})