Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Avoid mutating the global Promise #135

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
13 changes: 8 additions & 5 deletions src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ function toFastProperties(obj) {
(function () {}).prototype = obj;
}

Object.keys(Promise.prototype).filter(isNotMethod).forEach(del(Promise.prototype));
Object.keys(Promise).filter(isNotMethod).forEach(del(Promise));
toFastProperties(Promise);
toFastProperties(Promise.prototype);
const PromiseCopy = Object.assign({}, Promise);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. This is not supported until Node 4.

Is there preferred method to checking in a polyfill?

PromiseCopy.prototype = Object.assign({}, Promise.prototype);

global.Promise = Promise;
Object.keys(Promise.prototype).filter(isNotMethod).forEach(del(PromiseCopy.prototype));
Object.keys(Promise).filter(isNotMethod).forEach(del(PromiseCopy));
toFastProperties(PromiseCopy);
toFastProperties(PromiseCopy.prototype);

global.Promise = PromiseCopy;
30 changes: 30 additions & 0 deletions test/environment-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { assert } from 'chai';
import Promise from 'bluebird';
import '../src/environment';


describe('environment', () => {
it('does not modify the Promise package', () => {
assert.notStrictEqual(Promise, global.Promise);
});

describe('global promise', () => {
it('has ES6 methods', () => {
assert.isFunction(global.Promise.prototype.then);
assert.isFunction(global.Promise.prototype.catch);
assert.isFunction(global.Promise.prototype.constructor);
});

it('has ES6 static methods', () => {
assert.isFunction(global.Promise.all);
assert.isFunction(global.Promise.race);
assert.isFunction(global.Promise.resolve);
assert.isFunction(global.Promise.reject);
assert.isFunction(global.Promise.cast);
});

it('removes extraneous methods', () => {
assert.isUndefined(global.Promise.promisifyAll);
});
});
});