Skip to content

Instantly share code, notes, and snippets.

@fadec
Last active September 14, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadec/52b614871290b616fd8d3c07d49fdb9f to your computer and use it in GitHub Desktop.
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.
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