I ran across strange problem while experimenting with ES2015 Promises:
var webdriver = require('webdriverio');
(new Promise(function (resolve, reject) {
var client = webdriver.remote({desiredCapabilities: {browserName: 'chrome'}}).init();
client.then(function () {
console.log(typeof client.end); // outputs "function"
resolve(client)
}).catch(function (e) {
reject(e);
});
})).then(function (client) {
console.log(typeof client.end); // outputs "undefined"
}).catch(function (e) {
console.log(e);
});
In the code above, some kind of magic happens, when I resolve client
. Before I call resolve, client contains state=fulfilled
and value
properties, together with methods like then, end, click, waitForExist etc. But in callback, I receive as a parameter only the value
property of original client object. My question is simple, what kind of sorcery ES2015 Promise performs when resolving such object?
Contrary to this oddly behaviour, calling resolve({client})
works as expected - then((result) => result.client.end())