1.0.1 • Published 3 years ago

callback-then v1.0.1

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

Name

Easily convert a callback-based function to a promise in TypeScript

Installation

npm i --save callback-then

Usage

Supports both ESM and CommonJS

// esm
import toPromise from 'callback-then`
// commonjs
const toPromise = require('callback-then').default

Example Usage

import toPromise from 'callback-then`

const result = await toPromise(cb => {
  setTimeout(() => cb(null, 'success'), 10)
}) // resolves to 'success'

await toPromise(cb => {
  setTimeout(() => cb(), 10)
}) // resolves undefined

await toPromise(cb => {
  setTimeout(() => cb(new Error('boom')), 10)
}) // rejects with Error: boom

TypeScript Example Usage

import toPromise from 'callback-then`

const result = await toPromise<string>(cb => {
  setTimeout(() => cb(null, 10), 10)
}) // resolves to 'success' and type is a string

License

MIT

1.0.1

3 years ago

1.0.0

3 years ago