0

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())

kubajz
  • 61
  • 7
  • So your `client` is a thenable? – Bergi Jan 21 '16 at 18:45
  • First of all, avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Jan 21 '16 at 18:46
  • @Bergi yes, it is. I understand, this is just for the purpose of example. I'm really interested why there is such behaviour. – kubajz Jan 21 '16 at 19:54
  • If just for the question, using `Promise.resolve(client)` will have the same behaviour – Bergi Jan 21 '16 at 20:45
  • I'm not sure I understand what exactly you want to know. Are you interested in *what exactly happens*, or *why* this happens? If the latter, have a look at http://stackoverflow.com/q/29435262/1048572, http://stackoverflow.com/a/30852423/1048572, http://stackoverflow.com/a/31327725/1048572 – Bergi Jan 22 '16 at 15:51
  • Or alternatively, have a look at [this one](http://stackoverflow.com/q/34964819/1048572). I guess I'm gonna close as a dupe of some if you don't object. – Bergi Jan 23 '16 at 15:51

1 Answers1

0

This is a standard behaviour in promise chaining, which means ES2015 promise library recognises that you're resolving a promise with another promise and waits for that promise to resolve, then continues with the chain, which is why in the callback, you get the actual value instead of the promise object you resolved with earlier.

In the second case however, you're wrapping the promise in an object which prevents promise library to recognise it as a promise, so it just passes it to the next callback.

hmd.ai
  • 1,163
  • 11
  • 13