Comments (4)
looking at davy, it looks like it directly accesses this.vaule and this.deferreds so it does not have private states.
from native-promise-only.
However I think it is possible to have immutability without a private state using Object.defineProperty.
var obj = {};
Object.defineProperty(obj, 'prop', {
writable: false,
enumerable: false,
configurable: false,
value: 123
});
console.log(obj.prop); // 123
obj.prop = 1;
console.log(obj.prop); // 123
delete obj.prop;
console.log(obj.prop); // 123
Therefore if my understanding is correct by doing this:
function Promise() {
var somePrivateState;
Object.defineProperty(this, '_then', {
writable: false, configurable: false, enumerable: false,
value: function() {
// actual then function using somePrivateState
}
};
}
Promise.prototype.then = function () {
return this._then.apply(this, arguments);
};
You will not have external mutations of promises. This would seem to be a better trade-off.
from native-promise-only.
Yes, that's conceptually kinda similar to what I'm already doing, except you put a public then
on the Promise.prototype
to point to internal state, whereas I just had it internal already. Mostly the same end result.
The difference is you have a public prototype then
which mine only has then
on the instance. Mine could easily do that, but the question is, how would someone do sub-classability (both in ES5 and ES6) with this pattern? I believe the problem will be sub-classes will also not be able to access the private state, which one would almost certainly expect.
Let's explore that.
In ES5:
function CustomPromise(fn) {
var morePrivateState = { .. };
Promise.call(this,fn);
Object.defineProperty(this, "_customThen", {
writable: false, configurable: false, enumerable: false,
value: function() {
// other custom stuff with `morePrivateState`
// this._then.apply(this, arguments);
// the problem is I have no access to `somePrivateState` from parent, so I may be quite
// limited in what I can do here.
// At best, this is limited (and potentially subtly so) sub-classing.
}
);
}
CustomPromise.prototype = Object.create(Promise.prototype);
CustomPromise.prototype.customThen = function() {
return this._customThen.apply(this,arguments);
};
var myPromise = new CustomPromise(myResolver);
myPromise.customThen(success,failure);
// ...
In ES6:
class CustomPromise extends Promise {
CustomPromise(fn) {
var morePrivateState = { .. };
super(fn);
Object.defineProperty(this, "_customThen", {
writable: false, configurable: false, enumerable: false,
value: function() {
// other custom stuff with `morePrivateState`
// this._then.apply(this, arguments);
// the problem is I have no access to `somePrivateState` from parent, so I may be quite
// limited in what I can do here.
// At best, this is limited (and potentially subtly so) sub-classing.
}
);
}
customThen() {
return this._customThen.apply(this,arguments);
}
}
var myPromise = new CustomPromise(myResolver);
myPromise.customThen(success,failure);
// ...
In both ES5 and ES6 syntaxes, it comes down to having the state partitioned between Promise
(parent) and CustomPromise
(child). If customThen(..)
or _customThen(..)
needed to update the pending state, or the value, or something... they cannot.
They are limited to only being able to track and modify their own child state, but they would probably need to be modifying the "inherited" parent-private state, which they cannot.
In practice, I believe this would basically devolve to fully re-specifying behavior on the CustomPromise.prototype
and fully re-specifying all state behavior in the CustomPromise
constructor closure.
The other option from the above code is to avoid calling the parent constructor, which thus avoids creating the parent private state AND it avoids creating any _then
to use, which means you would end up re-specifying the internal state AND the implementation of _then
down on the child, which is again basically making the sub-classing almost entirely moot.
In other words, in practice, there would be little to no benefit to being able to sub-class. You can, but it doesn't get you anything substantial.
Make sense?
from native-promise-only.
I feel pretty confident that from the polyfill standpoint, this is a trade-off, and not one I'm willing to make. Promise immutability is more important than sub-classing.
The only other way to keep state private would be to use a WeakMap which is not practically polyfillable (its performance would be unacceptable). That's great for reference implementations, but unacceptable for a polyfill.
from native-promise-only.
Related Issues (20)
- browsers support information HOT 7
- Cross-Browser Support & Tests HOT 3
- Support IE8 HOT 5
- What is correct execution sequence when attach multiple handlers to the same promise. HOT 8
- Better native support detection? HOT 3
- Create tag for v0.7.6-a HOT 1
- Android 4.0 issue HOT 17
- AMD? HOT 19
- handling uncaught exceptions HOT 5
- Promise.resolve(1) throws on Android 4.0 HOT 1
- `resolve(..)` should call a thenable's `then(..)` async, not sync. HOT 5
- Unhandled rejection detection HOT 9
- Suggestion: make Promise.prototype aliases of `then` and `catch` HOT 5
- Error when running through babel compiler HOT 5
- Module with unminified code should be exported by default
- on nodejs, exceptions are not shown. HOT 4
- Microtasks? HOT 1
- Ponyfill support? HOT 1
- Missing tag for version 0.7.8-a used by jQuery HOT 7
- Promise finally not supported HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from native-promise-only.