handle-async

1.1.0 • Public • Published

handle-async NPM version Downloads Build Status

Resolves any function to a value. Generators, callbacks, promises, flat values - don't worry about it!

Pass in any function, and we'll make sure you get the value how you need it.

Install

npm install handle-async --save

Example

Assume these input functions for the following examples:

// good ol' function that returns a value
const flat = () => 123

// calling a callback with the value
const cb = (cb) => cb(null, 123)

// creating an async function that resolves the value
const asyncd = async () => 123

// a function that returns a promise with the resolved value
const promised = () => new Promise((reject, resolve ) => resolve(123))

Callbacks

import { callbackify } from 'handle-async'

callbackify(flat, (err, res) => console.log(res)) // 123
callbackify(cb, (err, res) => console.log(res)) // 123
callbackify(asyncd, (err, res) => console.log(res)) // 123
callbackify(promised, (err, res) => console.log(res)) // 123

Promises

Same thing, but returning promises so you can use async await.

import { promisify } from 'handle-async'

console.log(await promisify(flat)) // 123
console.log(await promisify(cb)) // 123
console.log(await promisify(asyncd)) // 123
console.log(await promisify(promised)) // 123

Same code, way different functions. Nice and easy.

Readme

Keywords

none

Package Sidebar

Install

npm i handle-async

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

4.75 kB

Total Files

4

Last publish

Collaborators

  • yocontra