Skip to content

Consume a callback without complecting handling and errorHandling

License

Notifications You must be signed in to change notification settings

pirfalt/consume

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Consume

Consume a callback without complecting handling with errorhandling.

Because functions that only do one thing is better than functions that does two. And honestly how many times did you forget to write if (err) /* Handle it */ at the top of a callback.

Example

fs.readFile('../package.json', consume(handleError, logPackage))

function logPackage(buffer) {
  var json = JSON.parse(buffer.toString())
  console.log(
    json.name, '\n',
    json.description, '\n',
    json.version, '\n',
    json.license.type,'\n'
    )
}

function handleError(error) {
  console.log(error); // Not that handled, but you get the point
}

Docs

type Callback<T>: (Error, T) => void

consume(errorHandler, successHandler)

consume( (Error) => void, (T) => void ) => Callback<T>

Thats all. Technically both the errorHandler and the successHandler are optional, but you don't get anywhere without a success handler. And you have to type consume(null, onSuccess) to ignore the error, which should make you think twice.

Continuable

As a companion to continuable, consume lets you take that last step and use single purpose, composable functions to the end.

License MIT

About

Consume a callback without complecting handling and errorHandling

Resources

License

Stars

Watchers

Forks

Packages

No packages published