stefanpenner / es6-promise

A polyfill for ES6-style Promises
MIT License
7.29k stars 594 forks source link

promise.then() chaining not working as expected in Phantomjs #178

Closed ioctaptceb closed 8 years ago

ioctaptceb commented 8 years ago

Hi,

Was using this library to polyfill phantomjs's lack of promise implementation. However, i came across the problem that chaining multiple then's results in the following error

Setup:

const somePromise = () => Promise.resolve('some static value');
const doSomething = (result) => {
  console.log(result);
  return result
}

const doOtherFunction = (result) => {
  console.log(result);
  return result;
}

somePromise().then(doFunction).then(doOtherFunction);

the first then statement acts as expected. doFunction will log 'some static value'. The second then statement, however, will log undefined.

Any thoughts on why this might be happening?

tylercollier commented 8 years ago

@ioctaptceb Did you find an answer to this? I'm seeing that none of my promises are running their then methods in PhantomJS 2.1.1.

ioctaptceb commented 8 years ago

@tylercollier there are a few factors, but I didnt have time to fully investigate. It looks like the initial return from the promise (the one your return with resolve) is the return value used for future chained promises. If you specify a different return value from a function in .then(), the function will be run immediately. I took a look at the code and unfortunately this isnt what (from my brief reading) should be the case, because it seems that it should return the return of the then. However I found that if you always return the resolve value of the promise, it works as expected. I didnt have time to dig in deeper so i just organized my code to always return the resolved value, so that it could be tested.

as for what you are seeing, I am not sure if it is the same, what does your code look like?

tylercollier commented 8 years ago

I'm so sorry, I typed the following last night but forgot to press the send button. So, there is no issue for me and this case can be closed (unless you want it open for yourself).

My problem was I wasn't calling my mocha it block's done() method.

stefanpenner commented 8 years ago

given:

require('es6-promise');

function somePromise() {
  return Promise.resolve('some static value');
}

function doFunction(result) {
  console.log(result);
  return result;
}

function doOtherFunction(result) {
  console.log(result);
  return result;
}

somePromise().then(doFunction).then(doOtherFunction);

and

browserify test.js -o out.js
phantomjs out.js

the output is:

some static value
some static value
stefanpenner commented 8 years ago

This seems true for phantom 1.9.2 and 2.x.

Please runnable a reproduction, that demonstrates this issue and I can reopen.