Skip to content

A small JS helper function for dealing with callbacks and an optional options argument.

License

Notifications You must be signed in to change notification settings

fritzmonkey/optional-options

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

optional-options

A small JS helper function for dealing with callbacks and an optional options argument.

Usage Examples

const optOpts = require('optional-options')();

function foo(options, callback) {
  ({options, callback} = optOpts(options, callback));

  callback(options.name);
}

foo(function(output) {
  console.log(output); // undefined
});

foo({ name: 'buzz' }, function(output) {
  console.log(output); // 'buzz'
});

Keys can also be provided.

const optOpts = require('optional-options')('opts', 'cb');

function foo(options, callback) {
  const { opts, cb } = optOpts(options, callback);

  cb(opts.name);
}

foo(function(output) {
  console.log(output); // undefined
});

foo({ name: 'buzz' }, function(output) {
  console.log(output); // 'buzz'
});

About

A small JS helper function for dealing with callbacks and an optional options argument.

Resources

License

Stars

Watchers

Forks

Packages

No packages published