I've written a library that is completely separate from Promise API but achieves similar goals. It uses window.requestAnimationFrame
and falls back to setTimeout and has nothing common with Promises. In fact you can run it on ie9 - 10 or a machine from 2009 etc. Here is the source code
How is it possible that the below code works, and the 2nd promise gets the value (v + 3) correctly into the 3rd promise after 10 second delay?? Because rafx.async...
returns 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 expectation would be that the v at x.then(v...
would equal to whatever custom object returned from the custom then
.
Here is what rafx.async(6).skipFrames(600)...then(v => v + 3)
returns:
prt.Thenable {....
status: Status {thenable: prt.Thenable, instance: Rafx...
_argObj: {value: 10, done: true},
_breaker: {value: false}
....
The Thenable
and Status
constructors have nothing to do with Promises
, they are completely custom.
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 verify that Promise.prototype.then !== rafx.Thenable.prototype.then
, the then
implementation is completely separate, as seen here;
So how is it possible that Promise
understands how my API works?????
(I am sure I am missing something awfully clear)
PS: I replaced all arrow functions (coz of this
binding) with regular ones, it still works, but it shouldn't..