thenify

WebJar for thenify

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

thenify
Last Version

Last Version

3.3.1
Release Date

Release Date

Type

Type

jar
Description

Description

thenify
WebJar for thenify
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/thenables/thenify

Download thenify

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/thenify/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>thenify</artifactId>
    <version>3.3.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/thenify/
implementation 'org.webjars.npm:thenify:3.3.1'
// https://jarcasting.com/artifacts/org.webjars.npm/thenify/
implementation ("org.webjars.npm:thenify:3.3.1")
'org.webjars.npm:thenify:jar:3.3.1'
<dependency org="org.webjars.npm" name="thenify" rev="3.3.1">
  <artifact name="thenify" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='thenify', version='3.3.1')
)
libraryDependencies += "org.webjars.npm" % "thenify" % "3.3.1"
[org.webjars.npm/thenify "3.3.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : any-promise jar [1.0.0,2)

Project Modules

There are no modules declared in this project.

thenify

NPM version Build status Test coverage Dependency Status License Downloads

Promisify a callback-based function using any-promise.

  • Preserves function names
  • Uses a native promise implementation if available and tries to fall back to a promise implementation such as bluebird
  • Converts multiple arguments from the callback into an Array, also support change the behavior by options.multiArgs
  • Resulting function never deoptimizes
  • Supports both callback and promise style

An added benefit is that thrown errors in that async function will be caught by the promise!

API

fn = thenify(fn, options)

Promisifies a function.

Options

options are optional.

  • options.withCallback - support both callback and promise style, default to false.

  • options.multiArgs - change the behavior when callback have multiple arguments. default to true.

    • true - converts multiple arguments to an array
    • false- always use the first argument
    • Array - converts multiple arguments to an object with keys provided in options.multiArgs
  • Turn async functions into promises

var thenify = require('thenify');

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
});
  • Backward compatible with callback
var thenify = require('thenify');

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
}, { withCallback: true });

// somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
// somethingAsync(a, b, c, function () {});

or use thenify.withCallback()

var thenify = require('thenify').withCallback;

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
});

// somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
// somethingAsync(a, b, c, function () {});
  • Always return the first argument in callback
var thenify = require('thenify');

var promise = thenify(function (callback) {
  callback(null, 1, 2, 3);
}, { multiArgs: false });

// promise().then(function onFulfilled(value) {
//   assert.equal(value, 1);
// });
  • Converts callback arguments to an object
var thenify = require('thenify');

var promise = thenify(function (callback) {
  callback(null, 1, 2, 3);
}, { multiArgs: [ 'one', 'tow', 'three' ] });

// promise().then(function onFulfilled(value) {
//   assert.deepEqual(value, {
//     one: 1,
//     tow: 2,
//     three: 3
//   });
// });
org.webjars.npm

Thenables

Promise-based JavaScript Modules

Versions

Version
3.3.1
3.3.0
3.1.0