ts-promisify-callback
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

ts-promisify-callback

npm

Turns any function ending with the "popular" (Error, Result) callback format into a Promise. Type-safe!

Install

npm install ts-promisify-callback

Usage

IMPORTANT: This library requires typescript@^4.0 (currently in beta)!

import { promisifyCallback } from "ts-promisify-callback";
 
function helloWithCallback(
  name: string,
  age: number,
  cb: (err: Error | null, result: string) => void
) {
  cb(null, `I'm ${name}, age ${age}`);
}
 
let helloAsPromise = promisifyCallback(helloWithCallback);
// let helloAsPromise: (name: string, age: number) => Promise<string>
 
(async () => {
  let greeting = await helloAsPromise("hello", 31);
  alert(greeting);
})();

Contributing

This is a very simple library, but I'm sure there are many ways to improve it to be more flexible...

Pull Requests Welcome!

Package Sidebar

Install

npm i ts-promisify-callback

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

10.4 kB

Total Files

7

Last publish

Collaborators

  • cjonas