Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

p-from-callback

dotcore6471kSEE LICENSE IN LICENSE.md2.0.0TypeScript support: included

Map a callback to a promise

promises, callbacks

readme

p-from-callback

Build Status npm package Coverage Status

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).