unpromisify

automatically adapt callbacks whether they are node style or promise returning

Usage no npm install needed!

<script type="module">
  import unpromisify from 'https://cdn.skypack.dev/unpromisify';
</script>

README

unpromisify

automatically adapt callbacks whether they are node style or promise returning

Build Status Coverage Status Code Climate Dependency Status devDependency Status

NPM

usage

unpromisify(userCallback, ctx, args, done);
unpromisify(userCallback, args, done);
unpromisify(userCallback, done);
unpromisify(userCallback, opts);

  • userCallback: a user supplied callback that performs some async task. It may call a node style callback, or return a promise.

  • done or opts.done: your callback, which will be called after user supplied async task completes.

  • args or opts.arg: (optional) array of arguments to call userCallback with. A callback handler will automatically be appended as the last argument.

  • ctx or opts.ctx: call the user supplied callback with this context

  • opts.syncResults: by default, if userCallback returns a non-promise, it is assumed that the return value is the intended result. This is similar to Promise.resolve(value) or Q.when(value). To disable this feature, set opts.syncResults to false; non-promise return values will be ignored.

advantages

  • Allows you to write your code using standard node style callbacks, without forcing it on users who prefer to return promises.

  • Guarantees your callback will be called async. Preventing the release of Zalgo.

  • Guarantees your callback will only called once. A major benefit of promises is the guarantee that they can only be resolved once, and that your then handlers will only be called once. This prevents a common bug in node style callbacks where someone else's bad code might try to call your callback twice. unpromisify provides the same "only resolve once" guarantee, regardless which method your user chooses.

releasing

Use the cut-release cli utility.

  1. npm install -g cut-release

  2. cut-release

  3. follow the prompts