Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use object as promise constructor #183

Closed
ghost opened this issue Mar 25, 2016 · 1 comment
Closed

Don't use object as promise constructor #183

ghost opened this issue Mar 25, 2016 · 1 comment

Comments

@ghost
Copy link

ghost commented Mar 25, 2016

during fixing #176 the next code was introduced in commit 9d7e2da:

var promise     = $Promise.resolve(1)
      , FakePromise = (

     promise.constructor = {} // !! at this moment promise.constructor equals new object instance !!

     )[require('./_wks')('species')] = function(exec){ exec(empty, empty); };
    // unhandled rejections tracking support, NodeJS Promise without it fails @@species test
    return (isNode || typeof PromiseRejectionEvent == 'function') && 

     promise.then(empty) // !! here comes .then method which fails with 
                                      // TypeError: object is not a constructor

      instanceof FakePromise;

the reason promise.then method fails in V8 is this --V8 creates new Promise every time .then method is called: https://github.com/v8/v8/blob/611add52550510b6bc0b79f6ae9278786183405f/src/js/promise.js#L285
and what is especially important, if promise.constructor !== Promise, V8 tries to create new promise from whatever thing promise.constructor points to ( https://github.com/v8/v8/blob/611add52550510b6bc0b79f6ae9278786183405f/src/js/promise.js#L233 )! which is {} in case above! hence we have object is not a constructor TypeError.

@zloirock
Copy link
Owner

Yep, feature detection should fail because it's a V8 bug and it should be fixed in V8 ~ Chrome 51, see #159.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant