Created
July 1, 2016 22:23
-
-
Save homam/7157172739e828b7501eedd8c7dff42d to your computer and use it in GitHub Desktop.
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
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