Created
January 22, 2019 09:40
-
-
Save pre1ude/5f268f1c5b057c1aeeca491cb3f2da36 to your computer and use it in GitHub Desktop.
simplest chain operation with async enabled
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
const Chain = f => ({ | |
f, | |
write: v => { | |
f(() => console.log(v)) | |
return Chain(f) | |
}, | |
wait: ms => Chain(next => setTimeout(() => f(next), ms)) | |
}) | |
let obj = Chain(next => next()) | |
// test case: | |
// obj.write(4) | |
// obj.wait(1000) | |
// obj.write(4).write(3).write(2) | |
// obj.wait(1000).wait(2000).write(4) | |
// obj.wait(1000).write(1).wait(2000).write(2) | |
// obj.write(1).wait(1000).write(2) | |
// obj.wait(3000).write(1).write(2).wait(1000).write(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment