@atsushi_suzuki/promisify
v0.0.3
Published
convert node.js callback function into function that returns promise
Downloads
3
Readme
promisify
Convert node.js style function into function that returns promise.
Usage
$ npm i --save @atsushi_suzuki/promisify
import * as fs from "fs";
import * as p from "@atsushi_suzuki/promisify";
(async function () {
const readFile = p.fn(fs.readFile);
const data = await readFile("package.json", "utf-8");
console.log(data);
})();
API
function promisify(f: Function): (...args: any[]) => Promise
promisify node.js style function with single result.
usage:
const data = await p.promisify(fs.readFile)("package.json", "utf-8");
function promisifyn(f: Function): (...args: any[]) => Promise<any[]>
promisify node.js style function with multiple results.
usage:
const [bytesRead, buffer] = await p.promisifyn(fs.read)(fd, buf, offset, length, pos);
function call(f: Function, ...args: any[]): Promise
call node.js style function with single result.
usage:
const data = await p.call(fs.readFile, "package.json", "utf-8");
function calln(f: Function, ...args: any[]): Promise<any[]>
call node.js style function with multiple results.
usage:
const [bytesRead, buffer] = await p.calln(fs.read, fd, buf, offset, length, pos);
function wait(ee: EventEmitter, event: string): Promise
returns a promise to wait until specified event fires. with single event argument.
usage:
const data = await p.wait(rs, "data");
function waitn(ee: EventEmitter, event: string): Promise<any[]>
returns a promise to wait until specified event fires. with multiple event arguments.
usage:
const [code, signal] = await p.waitn(proc, "exit");
function waitAny(...defs: EventDefinition[]): Promise
returns a promise to wait until any of specified events fire.
type EventDefinition = [
EventEmitter,
{[event: string]: Function}
];
usage:
const data = await p.waitAny([conn, {
"data": (data) => data,
"close": () => Promise.reject(new Error("unexpected close")),
"error": (err) => Promise.reject(err),
}]);