I have developed a unique library that functions independently from the Promise API, yet achieves similar objectives. It utilizes window.requestAnimationFrame and fallbacks to setTimeout, having no similarities with Promises. Interestingly, it is compatible with older systems such as ie9 - 10 or machines from 2009. You can find the source code here.
Surprisingly, the following code operates smoothly, enabling the 2nd promise to transmit the value (v + 3) correctly to the 3rd promise after a delay of 10 seconds. This is due to rafx.async... returning a custom proprietary object.
const x = Promise.resolve()
.then(() => console.log("2nd promise"))
.then(() => {
//600 frames === 10 secs
return rafx.async(6)
.skipFrames(600)
.then(v => v + 1)
.then(v => v + 3);
});
console.log(x instanceof Promise);
x.then(v => console.log("3rd promise", v));
<script src="https://cdn.jsdelivr.net/npm/rafx"></script>
The expected outcome at x.then(v...) should align with whatever custom object is returned from the custom then method.
This is the result of rafx.async(6).skipFrames(600)...then(v => v + 3):
prt.Thenable {....
status: Status {thenable: prt.Thenable, instance: Rafx...
_argObj: {value: 10, done: true},
_breaker: {value: false}
....
The constructors Thenable and Status are completely distinct from Promises, being entirely customized.
To my surprise, this even works:
const x = Promise.resolve()
.then(() => console.log("2nd promise"))
.then(() => {
return rafx.async("hello")
.loop(function(str){
return str;
})
.until(str => window.testVar === " world!")
.then(str => str + window.testVar);
});
console.log(x instanceof Promise);
x.then((value) => console.log("3rd promise", value))
.catch((e) => console.log(e));
<script src="https://cdn.jsdelivr.net/npm/rafx"></script>
<button onclick="window.testVar = ' world!';">set testVar to ' world!'</button>
You can confirm that Promise.prototype.then !== rafx.Thenable.prototype.then, indicating complete separation in implementation, as shown here;
How does Promise comprehend the functionality of my API? (I must be overlooking something obvious)
PS: I replaced all arrow functions (due to this binding) with regular ones, and it still functions, albeit against expectations..