Skip to content

ChuckJonas/ts-promisify-callback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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!

About

Turns any function ending with the standard "Error, Result" callback format into a promise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published