I've been through a lot of posts over the web(and in SO) saying that it is not possible. However, I did the following code:
var a = {
_sum: 0,
_timer: null,
_resetTimer: function() {
if (this._timer) {
window.clearTimeout(this._timer);
}
this._timer = window.setTimeout(this._endChain.bind(this), 0);
},
_endChain: function() {
console.log(this._sum);
},
A: function() {
this._resetTimer();
this._sum+= 1;
return this;
},
B: function() {
this._resetTimer();
this._sum+= 2;
return this;
}
};
a.A().B().B().A();
I want to know how problematic this can be, if this is not encouraged to be used.
Edit:
A negative point is: if you need anything that would be processed by _endChain you could not use return, only with a promise or another timeout outside the whole chain. Example.