Created
August 21, 2022 12:23
-
-
Save VladSez/a6e5753937cf995b55e8241c34500756 to your computer and use it in GitHub Desktop.
Interview question
This file contains 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
// 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