Last active
September 14, 2016 19:39
-
-
Save fadec/52b614871290b616fd8d3c07d49fdb9f to your computer and use it in GitHub Desktop.
How to subclass ES6 Promises with customized constructor. 99% of the time, you shouldn't.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValueInTime | |
{ | |
constructor (value, time) | |
{ | |
this.result = new Promise ((fulfill, reject) => { | |
this.fulfill = fulfill | |
this.reject = reject | |
}) | |
this.value = value | |
this.time = time | |
this.begin() | |
} | |
then () | |
{ | |
return this.result.then.apply(this.result, arguments) | |
} | |
begin () | |
{ | |
setTimeout(() => this.fulfill(this.value), this.time * 1000) | |
} | |
} | |
async function test () | |
{ | |
for (let n=0; n<3; ++n) | |
console.log(await new ValueInTime(n, 1)) | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment