Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement