p-from-callback
v2.0.0
Published
Map a callback to a promise
Downloads
44,145
Readme
p-from-callback
Map node-style callback to a promise
Install
$ npm install p-from-callback
Features
- pure esm module
- typescript types
- supports multiple return values
- 100% test coverage
Usage
import { readFile } from 'node:fs';
import fromCallback from 'p-from-callback';
fromCallback(cb => readFile("foo.txt", cb)).then(buf => buf.toString('utf8'));
fromCallback(cb => cb(undefined, "foo", "bar"), true)
.then([foo, bar] => foo === "foo" && bar === "bar");
API
fromCallback<R>((cb: (err: any, res: R)): any): Promise<R>
fromCallback<R>((cb: (err: any, ...res: R[])): any, true): Promise<R[]>
Takes a callback resolver and returns a promise to the resolved value, or array of values when multi = true
.
License
See the LICENSE file for license rights and limitations (MIT).