Skip to content

Instantly share code, notes, and snippets.

@VladSez
Created August 21, 2022 12:23
Show Gist options
  • Save VladSez/a6e5753937cf995b55e8241c34500756 to your computer and use it in GitHub Desktop.
Save VladSez/a6e5753937cf995b55e8241c34500756 to your computer and use it in GitHub Desktop.
Interview question
// Timer interview question
// https://twitter.com/erikras/status/1560681872002277379
class Timer {
q = Promise.resolve()
call(cb) {
this.q = this.q.then(() => cb())
return this
}
waitFor(seconds) {
this.q = this.q.then(() => new Promise((resolve) => setTimeout(resolve, seconds * 1000)))
return this
}
}
new Timer()
.call(() => console.log('Wait for 2 seconds'))
.waitFor(2)
.call(() => console.log('Wait for 3 seconds'))
.waitFor(3)
.call(() => console.log('Done'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment