Skip to content

Instantly share code, notes, and snippets.

@pre1ude
Created January 22, 2019 09:40
Show Gist options
  • Save pre1ude/5f268f1c5b057c1aeeca491cb3f2da36 to your computer and use it in GitHub Desktop.
Save pre1ude/5f268f1c5b057c1aeeca491cb3f2da36 to your computer and use it in GitHub Desktop.
simplest chain operation with async enabled
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