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

Do not monkey patch the global Promise. #304

Closed
despairblue opened this issue Jun 1, 2016 · 7 comments
Closed

Do not monkey patch the global Promise. #304

despairblue opened this issue Jun 1, 2016 · 7 comments
Labels

Comments

@despairblue
Copy link

This breaks unhandledRejection handler for example.

$ node
> Promise
[Function: Promise]
> require('jszip')
{ [Function: JSZip]
  support: 
   { base64: true,
     array: true,
     string: true,
     arraybuffer: true,
     nodebuffer: true,
     uint8array: true,
     blob: false,
     nodestream: true },
  defaults: 
   { base64: false,
     binary: false,
     dir: false,
     createFolders: true,
     date: null,
     compression: null,
     compressionOptions: null,
     comment: null,
     unixPermissions: null,
     dosPermissions: null },
  loadAsync: [Function],
  external: 
   { Promise: 
      { [Function: lib$es6$promise$promise$$Promise]
        all: [Function: lib$es6$promise$promise$all$$all],
        race: [Function: lib$es6$promise$promise$race$$race],
        resolve: [Function: lib$es6$promise$promise$resolve$$resolve],
        reject: [Function: lib$es6$promise$promise$reject$$reject],
        _setScheduler: [Function: lib$es6$promise$asap$$setScheduler],
        _setAsap: [Function: lib$es6$promise$asap$$setAsap],
        _asap: [Function: asap] } } }
> Promise
{ [Function: lib$es6$promise$promise$$Promise]
  all: [Function: lib$es6$promise$promise$all$$all],
  race: [Function: lib$es6$promise$promise$race$$race],
  resolve: [Function: lib$es6$promise$promise$resolve$$resolve],
  reject: [Function: lib$es6$promise$promise$reject$$reject],
  _setScheduler: [Function: lib$es6$promise$asap$$setScheduler],
  _setAsap: [Function: lib$es6$promise$asap$$setAsap],
  _asap: [Function: asap] }
> 
@despairblue despairblue changed the title Do not monkey patch the global Promise object. Do not monkey patch the global Promise. Jun 1, 2016
@dduponchel
Copy link
Collaborator

This wasn't intentional. This comes from the Promise implementation, see stefanpenner/es6-promise/issues/140 and stefanpenner/es6-promise/issues/141. Either we find a way to disable this or we change the implementation.

@dduponchel dduponchel added the bug label Jun 1, 2016
@despairblue
Copy link
Author

@dduponchel ah I see. And sorry for having been so terse 😃

This is how I currently fix this:

const p = Promise
require('jszip')
global.Promise = p

@puzrin
Copy link
Contributor

puzrin commented Jun 17, 2016

@dduponchel see https://github.com/kevinbeaty/any-promise. It allows user to select prefered promise library.

@dduponchel
Copy link
Collaborator

any-promise looks like a good option for nodejs, but I don't see an easy way to use it on an old brower (I get a any-promise browser requires a polyfill or explicit registration e.g: require('any-promise/register/bluebird')). Registering an implementation is not a good idea as a library and asking for a polyfill defeats the purpose of having an implementation in JSZip.external.Promise.

es6-promise should improve the situation in the future v4 but I don't know when it will be released (the code on their master branch still automatically call polyfill()). A fork + patch could work while waiting on this v4.

The other solution is to change the implementation. vow or lie look like good alternatives (working in IE 6, lightweight implementation).

Any ideas ?

dduponchel added a commit to dduponchel/jszip that referenced this issue Jul 24, 2016
Importing `es6-promise` had a side effect: this library replaces the
global Promise object (Stuk#304) (or tries to, Stuk#309). This is an intended
side effect from this library and while its version 4 should give us a
switch for this behavior, I don't know when it will be out (the master
branch of this project still auto replace the Promise).

I replaced it by `vow` which match the requirements:
- a Promise implementation
- a lightweight one
- works in IE6
- doesn't have too many dependencies

If a global Promise already exists, prefer it: a native promise is
likely to be better integrated anyway (unhandledRejection in node) and
some libraries (zone.js for example) replace global objects (Stuk#303).

I think it is enough for a semver minor version.

Fix Stuk#303 Stuk#304 Stuk#309
@dead-claudia
Copy link

@dduponchel It appears that lie is generally API-compatible with ES6 promises, so that might help.

@puzrin
Copy link
Contributor

puzrin commented Jul 30, 2016

@dduponchel as far as i remember, anypromise will use window.Promise in browser. IMHO it`s normal to say "you need Promise polyfill for old browsers" - it's the same as for es5 polyfills. Of cause, it's possible to create 2 web builds - pure one and with bulit-in promise lib.

@dduponchel
Copy link
Collaborator

Fixed in v3.1.0.

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

No branches or pull requests

4 participants