Skip to content
/ ts-pfy Public

Transforms callback-based function into a Promise-based (TypeScript-friendly)

License

Notifications You must be signed in to change notification settings

m18ru/ts-pfy

Repository files navigation

NPM DevDependencies

ts-pfy

Transforms Node.js-style callback-based function into a Promise-based.

This implementation is TypeScript-friendly.

Returns a function that will wrap the given nodeFunction. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument.

If the nodeFunction calls its callback with multiple success values, the fulfillment value will be the first fulfillment item.

Setting multiArgs to true means the resulting promise will always fulfill with an array of the callback's success value(s). This is needed because promises only support a single success value while some callback API's have multiple success value. The default is to ignore all but the first success value of a callback function.

If you pass a thisArg, the nodeFunction will be called as a method on the thisArg.

Install

npm install --save ts-pfy

Usage

function promisify(
	nodeFunction: Function,
	options?: Partial<PromisifyOptions>,
): Function;

Example:

import * as Fs from 'fs';
import pfy from 'ts-pfy';

const readFile = pfy( Fs.readFile );

readFile( 'test.txt' ).then( ( value ) => value.byteLength );

Options

thisArg

Type: any Default: null

A this context to use with function.

multiArgs

Type: boolean Default: false

Should multiple arguments be returned as an array?

With false only a first argument are returned in Promise.

License

MIT.

About

Transforms callback-based function into a Promise-based (TypeScript-friendly)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published