Can I use as a 1:1 replacement of window.Promise?
wintercounter opened this issue · 2 comments
I'd be really curious to try it on a Promise heavy app.
UPDATE:
I tried in a React application, I've got an infinite loop unfortunately :(
Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
Excellent question. Please see this section of the README.
I am not suprised that you got an infinite loop. In order to maximize performance, SPromiseMeSpeed does not delay until the next tick before executing the handle:
console.log("before");
SPromise.resolve().then(function(){
console.log("inside");
});
console.log("after");
Will yield:
before
inside
after
Whereas an ordinary browser promise
console.log("before");
Promise.resolve().then(function(){
console.log("inside");
});
console.log("after");
will yield:
before
after
inside
Coding using SPromise has to be written with the mindset that the delay is not guaranteed. Your react app is probably getting stuck because the code inside the then
executes sooner than expected. Try moving your SPromises closer to the end of statements to reduce potential chronological dependencies.
Further, SPromise reserves the right to switch up the order of execution of deeply nested Promises in order to prevent stack overflows, and this allows for the infinite loop you are experiencing. When SPromise detects too many nested levels of promises (creating a promise inside the then
inside the then
inside more then
s), it trampolines the stack and defers all further execution of then
s to the outmost SPromise to avoid a stack overflow.
That being said, I don't think SPromise is a danger to most projects (in terms of increasing the liklihood of bugs). You just have to approach SPromise the right way, and your code will be fine. Infact, I would argue that SPromise sometimes makes things easier to debug because interwoven asynchronous stack traces can sometimes be hard to follow and SPromises eliminates all possible asynchrony.
I hope this helps.
Thanks for the clear explanation.