Skip to content

Instantly share code, notes, and snippets.

@homam
Created July 1, 2016 22:23
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 homam/7157172739e828b7501eedd8c7dff42d to your computer and use it in GitHub Desktop.
Save homam/7157172739e828b7501eedd8c7dff42d to your computer and use it in GitHub Desktop.
class Callback {
constructor(f) {
this.run = (...args) => {
f(...args)
}
this.map = g => new Callback((...args) => {
const nArgs = R.init(args)
const callback = R.last(args)
nArgs.push((error, ...args) => {
if(!!error) {
callback(error, null)
} else {
callback(null, g(...args))
}
})
f(...nArgs)
})
this.bind = g => new Callback((...args) => {
const nArgs = R.init(args)
const callback = R.last(args)
nArgs.push((error, ...args) => {
if(!!error) {
callback(error, null)
} else {
g(...args).run(null, callback)
}
})
f(...nArgs)
})
}
}
new Callback((x, cb) => cb(null, x))
.map(x => x * 3)
.bind(x => new Callback((y, cb) => cb(null, x - 1)))
.run(6, (error, result) => console.log(error, result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment