handle-async
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.